Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the frequency of a string for each row in R

Tags:

r

count

frequency

I have a .txt file that looks something like this:

rs1 NC AB NC     
rs2 AB NC AA  
rs3 NC NC NC  
...  

For each row, I would like to count the frequencies of "NC", so that my output will be something like below:

rs1 2  
rs2 1  
rs3 3  
...

Can someone tell me how to do this in R or in Linux? Many thanks!

like image 580
Renee Avatar asked Sep 16 '15 20:09

Renee


People also ask

How do I count the frequency of a string in R?

You can use sapply() to go the counts and match every item in counts against the strings column in df using grepl() this will return a logical vector ( TRUE if match, FALSE if non-match). You can sum this vector up to get the number of matches.

How do you count the frequency of a column?

Note: You also can use this formula =COUNTIF(A1:A10,"AAA-1") to count the frequency of a specific value. A1:A10 is the data range, and AAA-1 is the value you want to count, you can change them as you need, and with this formula, you just need to press Enter key to get the result.


1 Answers

df$count <- rowSums(df[-1] == "NC")
#    V1 V2 V3 V4 count
# 1 rs1 NC AB NC     2
# 2 rs2 AB NC AA     1
# 3 rs3 NC NC NC     3

We can use rowSums on the matrix that is created from this expression df[-1] == "NC".

like image 135
Pierre L Avatar answered Sep 20 '22 07:09

Pierre L