I have the following dataset
A B C D
1! 0! 1! 0!
0! . 1! 1!
I need to print the column name to each match as follows
matches_1 matches_0
A,C B,D
C,D A
I'm using:
df$matches_1 <- colSums(apply(df, 1, stringr::str_count, "1!"))
to count occurences of 1! in each row, but i'm not sure how to print the column name instead of the sum
Using
==instead ofstringr, then extracting the matchingcolnames, finallypastethe result to get the desired output.