我正在寻找一种 RAM 有效的方法来在 data.table 的帮助下计算补集的中位数。
对于来自不同组的一组观察结果,我对“其他组”的中位数的实现感兴趣。即,如果一个 data.table 有一个值列和一个分组列,我想为每个组计算除当前组之外的所有其他组中值的中位数。例如,对于第 1 组,我们计算除属于第 1 组的值以外的所有值的中位数,依此类推。
一个具体的例子 data.table
dt <- data.table(value = c(1,2,3,4,5), groupId = c(1,1,2,2,2))
dt
# value groupId
# 1: 1 1
# 2: 2 1
# 3: 3 2
# 4: 4 2
# 5: 5 2
Run Code Online (Sandbox Code Playgroud)
我希望将medianOfAllTheOtherGroups 定义为第2 组的1.5 并定义第1 组的4,对同一data.table 中的每个条目重复:
dt <- data.table(value = c(1,2,3,4,5), groupId = c(1,1,2,2,2), medianOfAllTheOtherGroups = c(4, 4, 1.5, 1.5, 1.5))
dt
# value groupId medianOfAllTheOtherGroups …
Run Code Online (Sandbox Code Playgroud)