Consolidate duplicate rows

后端 未结 6 1918
暖寄归人
暖寄归人 2020-12-01 02:07

I have a data frame where one column is species\' names, and the second column is abundance values. Due to the sampling procedure, some species appear more than once (i.e.,

6条回答
  •  半阙折子戏
    2020-12-01 02:28

    > tapply(df$y, df$x, sum)
    sp1 sp2 sp3 sp4 
      2   9   7   3 
    

    if it has to be a data.frame Ben's answer works great. or you can coerce the tapply output.

    out <- tapply(df$y, df$x, sum)
    >     data.frame(x=names(out), y=out, row.names=NULL)
        x y
    1 sp1 2
    2 sp2 9
    3 sp3 7
    4 sp4 3
    

提交回复
热议问题