Here i want to add the column/'s in a dataframe and specifying the column name for which we found 1 in the main table.
e.g.
SrNo 1, we have 1 for the column C only hence the ouput table would have only one column and the value for it would be C.
SrNo 2, we have 1 for column B and D hence the ouput table should have 2 columns named as Ouput1,Ouput2 and the value for those would be B and D respectively.
i am able to identify that we can do this using below code and run this in a loop for all the rows, however it's not i want because of the performance issue.
Anyone has some good way to achieve this task
tableA <- data.frame(
SR = 1:5,
A = c(0, 0, 0, 1, 1),
B = c(0, 1, 0, 1, 1),
C = c(1, 0, 1, 1, 0),
D = c(0, 1, 0, 1, 1),
E = c(0, 1, 0, 0, 1))
colnames(tableA)[(which(tableA[1,] == 1))]
Table
Output should be like: