如何计算R中每一行的字符串的频率 [英] How to count the frequency of a string for each row in R

查看:182
本文介绍了如何计算R中每一行的字符串的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.txt文件,看起来像这样:

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

rs1 NC AB NC     
rs2 AB NC AA  
rs3 NC 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  
...

有人可以告诉我在R或Linux中做这个?非常感谢!

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

推荐答案

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

我们可以使用 rowSums 从此表达式创建 df [-1] ==NC

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

这篇关于如何计算R中每一行的字符串的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆