How to combine two rows in R?

后端 未结 3 1529
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 10:33

I would like to combine/sum two rows based on rownames to make one row in R. The best route might be to create a new row and sum the two rows together.

Example df:<

3条回答
  •  耶瑟儿~
    2021-01-06 11:04

    aggregate to the rescue:

    aggregate(df, list(Group=replace(rownames(df),rownames(df) %in% c("A","C"), "A&C")), sum)
    #  Group V2 V3 V4 V5
    #1   A&C  7 11  5  8
    #2     B  3  2  7  9
    #3     D  3  2  8  9
    

提交回复
热议问题