Consolidate duplicate rows

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

    A data.table solution for time and memory efficiency

    library(data.table)
    DT <- as.data.table(df)
    # which columns are numeric 
    numeric_cols <- which(sapply(DT, is.numeric))
    DT[, lapply(.SD, sum), by = x, .SDcols = numeric_cols]
    

    Or, in your case, given that you know that there is only the 1 column y you wish to sum over

    DT[, list(y=sum(y)),by=x]
    

提交回复
热议问题