I'd like to apply a function by rows on a data.frame to concatenate column titles depending on the value in the row.
1 TRUE TRUE
2 FALSE TRUE
3 FALSE FALSE
A B Result
1 TRUE TRUE A / B
2 FALSE TRUE B
3 FALSE FALSE NA
paste(names(df)[as.logical(df[i,])], collapse = ' / ')
We can use
MARGIN=1 to loop over the rows, subset the
names of the vector using the logical
vector as index and
paste them together.
df$Result <- apply(df, 1, FUN = function(x) paste(names(x)[x], collapse=" / "))