I want to convert R code to Python using ifelse loop [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This is the following R code.
mutate(Loan_Status_delinquency = ifelse(EOM_Delinq_Bucket %in% c(1, 2, 3, 4), "Yes", "No"))
I want this to be convert into Python
Thanks in advance.

You can try
df['result'] = df.Loan_Status_delinquency.isin([1,2,3,4]).replace({ True : 'Yes', False : 'No'})

df[['new_columm']] = df.Loan_Status_delinquency.apply(lambda x: "Yes" if x in [1,2,3,4] else "No")
Will add a column new_column onto your pandas dataframe df. In the future please have a go and we can look at your attempt - it'll mean you have to come here less often!

Related

How to find the percentage of observations in each day in R? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 days ago.
Improve this question
I have a dataset with time, where the time intervals are 6 hours apart and I have a column of heaterstatus.
The dataset :
I would like to know the percentage of zero occurred in each day for heaterstatus. New to R, any suggestion will be helpful.
Not tested since you only provided data as an image, but this should do what you want:
library(dplyr)
dat %>%
group_by(day = as.Date(Time)) %>%
summarize(pct_0 = mean(HeaterStatus == 0))

create an extra column based on 16 other numeric columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have 16 variables that are numeric, and I need to create an extra column that is YES (otherwise NO) when 3 or more variables out of those 16 have a value above 1015.
How could I do that?
Thanks
You can try with rowSums :
cols <- 1:16
df$res <- ifelse(rowSums(df[cols] > 1015, na.rm = TRUE) >= 3, 'Yes', 'No')

R: How to recode multiple values in dplyr? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How can I make the following changes for multiple or all variables?
change "yes" to 1
change "no" to 0
keep NAs
I tried recode but it seems not to be appliable to dataframes.
x <- data.frame(y=sample(c("yes", "no", "NA"), 10, replace = TRUE))
library(tidyr)
x$y2<- recode_factor(x$y, yes=1, no=0)

R: how to calculate if any of column in each row satisfies a condition without loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
dat = runif(10,1,10)
dat2 = runif(10,1,10)
dat3 = runif(10,1,10)
data = rbind(dat,dat2,dat3)
In the case of above data, I am wondering how I can filter out rows as long as there is one element in that row exceeding 5.
I know that I can use loop to achieve this, but I am wondering if there is more succinct way to do this.
try this:
data[apply(data>5, 1, sum)>0,]
This says "for condition having more than zero number exceeding 5 in each row, filter data".
data[do.call(pmax,data.frame(data))<=5,]
Cheers,
Bert

How do I access random elements from a vector in R studio? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say I have a vector c(1,2,3,4,5,6,7,8,9) how can I get a print of just 1,3,and 5 ?
For a sample of n random elements from vector X you can use sample(x = X, size = 3, replace = FALSE). To get the ith element of X you simply use X[i].

Resources