aggregating multiple columns in data.table

后端 未结 2 908
感情败类
感情败类 2020-12-15 20:55

I have the following sample data.table:

dtb <- data.table(a=sample(1:100,100), b=sample(1:100,100), id=rep(1:10,10))

I woul

相关标签:
2条回答
  • 2020-12-15 21:18

    this is actually what i was looking for and is mentioned in the FAQ:

    dtb[,lapply(.SD,mean),by="id"]
    
    0 讨论(0)
  • 2020-12-15 21:29

    I guess in this case is it fastest to bring your data first into the long format and do your aggregation next (see Matthew's comments in this SO post):

    library(data.table)
    dtb <- data.table(a=sample(1:100,100), b=sample(1:100,100), id=rep(1:10,10))
    library(reshape2)
    dt_long <- as.data.table(melt(dtb, id.var="id"))
    dt_long[, sum(value), by=c("id","variable")]
        id variable  V1
     1:  1        a 601
     2:  2        a 440
     3:  3        a 496
     4:  4        a 553
     5:  5        a 444
     6:  6        a 466
     7:  7        a 525
     8:  8        a 553
     9:  9        a 541
    ...
    
    0 讨论(0)
提交回复
热议问题