按组计算变量列表的总和 [英] Calculate sum of a list of variables by group
问题描述
我有一个data.table有一个键和约100个数字行,其中一个设置为键。
I have a data.table with one key and about 100 numeric rows, one of which is set to key. I would like to create a new variable that contains summation of each numeric rows, grouped by key.
例如,我的数据现在是
ID Count1 Count2 Count3
1 1 3 0
1 3 3 3
2 1 2 1
3 1 1 2
我想要的是:
ID Count1 Count2 Count3
1 4 6 3
2 1 2 1
3 1 1 2
我已经尝试了这么多方法来获得这个。我知道我可以做:
I have tried so many ways to get this. I know I can do:
Y <- X[, list(Count=sum(Count1), Count2=sum(Count2), Count3=sum(Count3), by = ID]
非常感谢您的帮助。
以下是生成测试数据的代码:
Here is a code to generate test data:
ID <-c(rep(210, 9), rep(3917,6))
Count1 <- c(1,1,0,1,3,1,4,1,1,1,1,1,1,0,1)
Count2 <- c(1,0,0,1,0,1,0,1,1,1,1,1,1,0,1)
Count3 <- c(1,0,0,1,0,1,0,1,1,1,1,1,1,0,1)
x <- data.table(ID, Count1, Count2, Count3)
setkey(x, ID)
推荐答案
不匹配您提供的示例,但不管怎样 - 您可以利用 data.table()
有一个名为 .SD的操作符
forsubset of data。This should work:
Your test data doesn't match the example you gave, but regardless - you can take advantage of the fact that data.table()
has an operator named .SD
for "subset of data. So this should work:
x[, lapply(.SD, sum), by = ID]
#----
ID Count Count2 Count3
1: 210 13 5 5
2: 3917 5 5 5
这实际上包含在FAQ:type vignette(datatable-faq,package =data .table)
或找到它在线。
This is actually covered in the FAQ: type vignette("datatable-faq", package="data.table")
or find it online.
这篇关于按组计算变量列表的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!