Row-wise sum of values grouped by columns with same name

后端 未结 2 760
-上瘾入骨i
-上瘾入骨i 2020-12-06 17:23

I have a data frame where several columns may have the same name. In this small example, both column \"A\" and \"G\" occur twice:

    A  C  G  A  G  T
1   1          


        
2条回答
  •  孤城傲影
    2020-12-06 17:48

    We can transpose dat , calculate rowsum per group (colnames of the original dat), then transpose the result back to original structure.

    t(rowsum(t(dat), group = colnames(dat), na.rm = T))
    #  A C G T
    #1 1 0 1 0
    #2 4 0 6 0
    #3 0 1 0 1
    #4 2 0 1 0
    #5 1 0 1 0
    #6 0 1 0 1
    #7 0 1 0 1
    

提交回复
热议问题