如何计算R中每行的字符串频率
我有一个.txt文件,看起来像这样:如何计算R中每行的字符串频率,r,count,frequency,R,Count,Frequency,我有一个.txt文件,看起来像这样: rs1 NC AB NC rs2 AB NC AA rs3 NC NC NC ... 对于每一行,我想计算“NC”的频率,以便我的输出如下: rs1 2 rs2 1 rs3 3 ... 有人能告诉我如何在R或Linux中做到这一点吗?非常感谢 dat <- read.table(text="rs1 NC AB NC rs2 AB NC AA rs3 NC NC NC") dat <- rbind(dat, d
rs1 NC AB NC
rs2 AB NC AA
rs3 NC NC NC
...
对于每一行,我想计算“NC”的频率,以便我的输出如下:
rs1 2
rs2 1
rs3 3
...
有人能告诉我如何在R或Linux中做到这一点吗?非常感谢
dat <- read.table(text="rs1 NC AB NC rs2 AB NC AA rs3 NC NC NC")
dat <- rbind(dat, dat, dat, dat)
如果您希望在所有行上具有聚合频率,请使用
rowSums(freq)
AA AB NC rs1 rs2 rs3
4 8 24 4 4 4
df$count谢谢!:)当然不是。如果df
是igraph映射,它也不起作用;如果df
是橙色切片或平板电视,它也不起作用。我不确定你的观点是什么。
rowSums(freq)
AA AB NC rs1 rs2 rs3
4 8 24 4 4 4
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