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

前端 未结 2 1438
刺人心
刺人心 2020-11-30 14:02

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 coun

2条回答
  •  误落风尘
    2020-11-30 14:50

    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".

提交回复
热议问题