I would like to create a new column which appends the suffix of column names together separated by space if the value is "TRUE". For example if I have this dataset:
df <- data.frame(
"category/var1" = c(TRUE, FALSE, TRUE),
"category/var2" = c(FALSE, TRUE, FALSE),
"category/var3" = c(TRUE, TRUE, FALSE)
)
# Print the dataframe
print(df)
category/var1 category/var2 category/var3
1 TRUE FALSE TRUE
2 FALSE TRUE TRUE
3 TRUE FALSE FALSE
I want to identify columns which have the same prefix "category/", and create a new column named "category" that appends the suffix of all column names if the values in that row are "TRUE".
I want the resulting new column to look like this:
category/var1 category/var2 category/var3 category
1 TRUE FALSE TRUE var1 var3
2 FALSE TRUE TRUE var2
3 TRUE FALSE FALSE var1
But I cannot seem to do this in an elegant way.