I have a data frame with the months June, May, July with various values. I am trying to create new columns for each month with new values. If the value is greater than or equal to 100 I would like it to be changed to 100 and in below 100 keep the value. Here is the df:
June <- c(100,50,200)
May <- c(250, 100, 20)
July <- c(20, 300, 20)
df <- data.frame(June, May, July)
and my goal is to have the df look like this:
June <- c(100,50,200)
May <- c(250, 100, 20)
July <- c(20, 300, 20)
June_new <- c(100,50,100)
May_new <- c(100,100,20)
July_new <- c(20,100,20)
df2 <- data.frame(June, May, July,June_new, May_new, July_new)
Thank you
How about this, using simple ifelse
to check condition and add columns to df itself.
df$new_June <- ifelse(df$June>=100, 100, df$June)
df$new_May <- ifelse(df$May>=100, 100, df$May)
df$new_July <- ifelse(df$July>=100, 100, df$July)
BEFORE df value:
> df
June May July
1 100 250 20
2 50 100 300
3 200 20 20
After running commands df value:
df$new_June <- ifelse(df$June>=100, 100, df$June)
df$new_May <- ifelse(df$May>=100, 100, df$May)
df$new_July <- ifelse(df$July>=100, 100, df$July)
df
June May July new_June new_May new_July
1 100 250 20 100 100 20
2 50 100 300 50 100 100
3 200 20 20 100 20 20
>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.