Aggregate data in one column based on values in another column

前端 未结 4 1710
轮回少年
轮回少年 2021-02-14 00:10

I know there is an easy way to do this...but, I can\'t figure it out.

I have a dataframe in my R script that looks something like this:

A      B    C
1.2         


        
4条回答
  •  悲&欢浪女
    2021-02-14 00:38

    Here is a solution using data.table for memory and time efficiency

    library(data.table)
    DT <- as.data.table(df)
    DT[, list(totalB = sum(B), num = .N), by = A]
    

    To subset only rows where C==1 (as per the comment to @aix answer)

    DT[C==1, list(totalB = sum(B), num = .N), by = A]
    

提交回复
热议问题