I am still new to R, and I run into this task to transform the df which I cannot solve.
I have a data frame, which I am trying to replace value of 1 (for each row) by its column name.
ID flag1 flag2 flag3 flag4
a1 1 0 1 1
a2 0 1 0 0
df <- data.frame("ID"=c("a1","a2"),
"flag1" = c(1,0),
"flag2" = c(0,1),
"flag3" = c(1,1),
"flag4" = c(1,1))
I understand that I will have to create a function and/or loop to accomplish my goal.
I am looking to transform the dataframe to the following
ID Name
a1 flag1
a1 flag3
a1 flag4
a2 flag2
Thank you for any help/hint.