I have a dataframe with the following fields
class bed_1 chair_1 bottle_1 table_1 ...
bed TRUE FALSE FALSE FALSE ...
chair FALSE TRUE FALSE FALSE ...
sofa FALSE FALSE TRUE FALSE ...
table FALSE FALSE FALSE FALSE ...
I want to compare class column with all other columns. The following is the expected output
class new_col
bed bed_1
chair chair_1
bottle bottle_1
table
So, essentially, I need to pickup column name with TRUE value for specific class.
The solution i tried takes long time due to large number of records, I am looking for an efficient way of doing this. Here is my solution.
new_df <- data.frame(class = df$class, new_col = NA)
for (row_n in 1:length(df$class) ){
indx <- which (df[row_n, ] == 'TRUE')
new_df$new_col[row_n] <- ifelse (length(indx) > 0, colnames(df)[idx], '')
}