I have the following dataset:
df <- data.frame("attribute"=c('name', 'age', 'location'),
"A"=c(1,0,0), "B"=c(0,1,1), "C"=c(1,0,1))
names(df) <- c("attribute", "A", "B", "C")
df
attribute A B C
name 1 0 1
age 0 1 0
location 0 1 1
- I want to add a new column in R that contains one of the column names among A, B C.
- The condition is that one of the attributes
name, age, location
should match with one of the columns A, B C. The first column name whose value of the attribute is one should be listed as an additional column as follows:
attribute A B C Column name 1 0 1 A age 0 1 0 B location 0 1 1 B
for(i in df$attribute){for(j in c("A", "B", "C")}print(names(df) [names(df)%in%c("A", "B", "C")][i]==1)
I am unable to solve this.