I have a df with 150 rows values, and I would like R to loop or otherwise repeat the following for each row and column in the df;
Example of data
df <- data.frame('criteria1' = c('x','1', 'X', '', 'X'), "criteria2" = c('y','3', '', 'X', ''), "criteria3" = c('y','7', '', 'X', 'X'))
If a X appears in a row, I would like the function to take the values from the two first rows, and paste them together with a '=' between them. The following works fine for the first column.
df$criteria1 <- ifelse(df$criteria1 == 'X', paste(df$criteria1 [1], '=', df$criteria1 [2]),'')
head(df)
The problem is when I try to do it for all the columns in the dataframe
df[] <- lapply(df, function(x) ifelse(df$x== 'X', paste(x[1], '=', x[2]),''))
All cells become NA. I have tried with different versions of the code above, but nothing gives the expected output which is;
head(data.frame('criteria1' = c('x','1', 'x=1', '', 'x=1'), "criteria2" = c('y','3', '', 'y=3', ''), "criteria3" = c('y','7', '', 'y=7', 'y=7')))