聚合 data.table 中的多个列 [英] aggregating multiple columns in data.table
本文介绍了聚合 data.table 中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下示例 data.table
:
dtb <- data.table(a=sample(1:100,100), b=sample(1:100,100), id=rep(1:10,10))
例如,我想使用 colSums
按 id 聚合所有列(a 和 b,尽管它们应该分开).这样做的正确方法是什么?以下方法不起作用:
I would like to aggregate all columns (a and b, though they should be kept separate) by id using colSums
, for example. What is the correct way to do this? The following does not work:
dtb[,colSums, by="id"]
这只是一个示例,我的表有很多列,所以我想避免在函数名中指定所有列
This is just a sample and my table has many columns so I want to avoid specifying all of them in the function name
推荐答案
这实际上是我一直在寻找的,并且在常见问题解答中提到:
this is actually what i was looking for and is mentioned in the FAQ:
dtb[,lapply(.SD,mean),by="id"]
这篇关于聚合 data.table 中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文