Search code examples
rdataframemultiple-columnssubstitution

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


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