如何从ave中的函数调用非x?

问题描述 投票:0回答:1

我有一些数据,我想为这些数据计算具有相同ID的所有行的总'数据'减去不共享相同ID的所有行的总'数据'。目前我已经找到第一部分(见代码),但不是第二部分。有没有人知道如何在ave内的函数中调用所有非x?

示例数据:

 group_ID_dets <- data.table("group_ID" = rep(c(1,2), 2), "n_IDS" = 1:4, "data"= c(1,5,10,100))

我的第1部分代码:

 group_ID_dets$totals <- ave(group_ID_dets$data, group_ID_dets$group_ID, FUN = function(x) sum(x))

我希望将其作为输出:

   group_ID n_IDS data totals
1:        1     1    1      0
2:        2     2    5     94
3:        1     3   10      0
4:        2     4  100     94
r function data.table
1个回答
2
投票

它非常草率,但使用数据表计算j表达式的方式,您可以执行以下操作:

group_ID_dets[, .(n_IDS, data, totals = max(c(2* sum(data) - sum(group_ID_dets$data), 0))), by = group_ID]

这使用DT[i, j, by]格式,其中j表达式

.(n_IDS, data, totals = max(c(2* sum(data) - sum(group_ID_dets$data), 0)))

j表达式表示我们将包含n_IDS列和数据列,然后我们将计算按group_ID列分组的总计列。

data.table评估是针对分组变量的,但是我们可以使用data.frame提取来提取整个列的总数并退出分组。所以sum(group_ID_dets$data)仍然评估整个总和。然后我们可以使用以下等式:a - sum(b,c,...)= a + a - (a + sum(b,c,...))= 2 * a - sum(a,b, C, ...)。根据您想要的输出,我们将负值保持为0。

   group_ID n_IDS data totals
1:        1     1    1      0
2:        1     3   10      0
3:        2     2    5     94
4:        2     4  100     94
© www.soinside.com 2019 - 2024. All rights reserved.