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

How to change the values of columns in a big dataframe using the names of variables?

I have this dataset (but let’s suppose it is very big with many rows and columns)

df = data.frame(x = c(1,0,0,1),
                y = c(0,0,1,1))

I wish to use the names of variables x, y, etc.. every time while substuting 1 and 0 with yes and no as follows :

df = data.frame(x = c('yes_x','no_x','no_x','yes_x'),
                y = c('no_y','no_y','yes_y','yes_y'))

would appreciate the halp. Thanks

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

>Solution :

In dplyr, with cur_column:

library(dplyr)
df %>% 
  mutate(across(x:y, 
                ~ ifelse(.x == 1, "yes", "no") %>% 
                  paste(cur_column(), sep = "_")))

In base R, with mapply:

df[] <- ifelse(df == 1, "yes", "no")
df[] <- mapply(paste, df, colnames(df), sep = "_")

output

      x     y
1 yes_x  no_y
2  no_x  no_y
3  no_x yes_y
4 yes_x yes_y
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