Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

variable mutation from numeric to character according to a condition

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading