I have a data frame imported from a tsv file with four columns, all digital numbers, with a few "NA" in column 4.
My goal was to create two extra columns in accordance with my current columns, so, column 5 is data as a replica to column 3, except for capping the values to [-5,5]; and then to create column 6 that gives "True" only when value in column 4 <0.05
I did the following, the commands all worked with no error message, however, my data only get capped to that df$C5 is not larger than 5, while it still contains elements smaller than -5, and the NA in df$C6 is not replaced. So, I used replace() for three times, one time it worked, the other two times did not work and showed no error and I am totally confused. Meanwhile, I am also wondering if there is alternative ways to better cap the data aside from replace() ?
Thank you so much!
df$C5 <- replace(df$C3,df$C3 >5, 5)
df$C5 <- replace(df$C3,df$C3 <-5, -5)
df$C6 <- df$C4 <0.05
df$C6 <- replace(df$C6,df$C6=="NA","False")
CodePudding user response:
You can see case_when
function in the dplyr
package.
library(dplyr)
df %>% mutate(C5 = case_when(C3 > 5 ~ 5,
C3 < -5 ~ -5,
T ~ C3),
C6 = case_when(C4 < 0.05 ~ TRUE,
T~ FALSE))