Consolidate duplicate rows

后端 未结 6 1929
暖寄归人
暖寄归人 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:31

    This works:

    library(plyr)
    ddply(df,"x",numcolwise(sum))
    

    in words: (1) split the data frame df by the "x" column; (2) for each chunk, take the sum of each numeric-valued column; (3) stick the results back into a single data frame. (dd in ddply stands for "take a d ata frame as input, return a d ata frame")

    Another, possibly clearer, approach:

    aggregate(y~x,data=df,FUN=sum)
    

    See quick/elegant way to construct mean/variance summary table for a related (slightly more complex) question.

提交回复
热议问题