In my data frame I have a numeric column var1. In that column, all the values that are 30 or less, I want to have as "<=30" and the variable type should be character.
This code sure recognizes values less than 30, and that results either TRUE or FALSE.
Mydata <- Mydata %>% select(
"var1") %>%
mutate(less_than_30 = (var1 <= 30))
With this clumsy code I tried to transform TRUE’s into "<=30", but that doesn’t work because ! Can't convert <character> to <logical>., though I think it should read <logical> to <character>
Mydata["less_than_30"][Mydata["less_than_30"] == "TRUE"] <- "<=30"
I’m sure there is a handy way to do this. Can you help?
>Solution :
Please try
Mydata <- Mydata %>% select(
"var1") %>%
mutate(less_than_30 = ifelse(var1 <= 30,'<=30',NA))