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

后端 未结 2 761
-上瘾入骨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
    
    0 讨论(0)
  • 2020-12-06 17:56

    We could split the dataframe by name using split.default and take the row-wise sum using rowSums to create one column for each unique name.

    sapply(split.default(df, names(df)), rowSums, na.rm = TRUE)
    
    #  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
    
    0 讨论(0)
提交回复
热议问题