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

前端 未结 2 1431
刺人心
刺人心 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:43

    dat <- read.table(text="rs1 NC AB NC rs2 AB NC AA rs3 NC NC NC")
    dat <- rbind(dat, dat, dat, dat)
    

    You can use a rowwise table to get the frequencies per row In this case for row 1 to 4 the frequencies that are equal as i copied the data

    freq <- apply(dat, 1, table)
        1 2 3 4 # row-number
    AA  1 1 1 1
    AB  2 2 2 2
    NC  6 6 6 6
    rs1 1 1 1 1
    rs2 1 1 1 1
    rs3 1 1 1 1
    

    If you want to have aggregated frequencies over all rows use

    rowSums(freq)
    AA  AB  NC rs1 rs2 rs3 
     4   8  24   4   4   4 
    

提交回复
热议问题