Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11781

print column name when specific row of a column contains string in R

$
0
0

I have the following dataset

A    B    C    D1!   0!   1!   0!0!   .    1!   1!

I need to print the column name to each match as follows

matches_1     matches_0A,C           B,DC,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


Viewing all articles
Browse latest Browse all 11781

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>