Merge rows with duplicate IDs

后端 未结 2 1305
礼貌的吻别
礼貌的吻别 2021-01-26 01:09

I would like to merge and sum the values of each row that contains duplicated IDs.

For example, the data frame below contains a duplicated symbol \'LOC102723897\'. I wou

相关标签:
2条回答
  • 2021-01-26 01:30

    You group-by on Symbol and sum all columns.

    library(dplyr)
    df %>% group_by(Symbol) %>% summarise_all(sum)
    

    using data.table

    library(data.table)
     setDT(df)[ , lapply(.SD, sum),by="Symbol"]
    
    0 讨论(0)
  • 2021-01-26 01:39

    We can just use aggregate from base R

    aggregate(.~ Symbol, df, FUN = sum)
    
    0 讨论(0)
提交回复
热议问题