Search code examples
rrstudiodata-analysisdata-cleaning

How to count the number of columns containing a particular string in r?


I have some data that has names and tags associated with those names. There are upto 94 tags for each name. Each tag is in a separate column. I need to count the number of columns that contain a particular string. How can I do that?

Edit: Sample data

https://i.sstatic.net/eJtwd.png

I want to count the number of columns that contain the string "WG".


Solution

  • This will return the number of columns containing "WG" row-wise.

    apply(X = df,MARGIN = 1,function(t){sum(grepl(pattern = "WG",x = t,fixed = TRUE))})