Search code examples
rmultiple-columnssubstitution

Changing the values of columns that include more than two values into values that include variable names?


I have this data set (supposing it is big with many rows and columns)

df = data.frame(x = c(1,2,3,4),
                y = c(2,4,5,6) )

and I want it to become like this using the names of variables x, y, etc ... with a simple function

df = data.frame(x = c('x_1','x_2','x_3','x_4'),
                y = c('y_2','y_4','y_5','y_6') )

appreciate the help


Solution

  • mapply(paste, colnames(df), df, sep = "_")