我正在寻找一种实用的方法来使用(最好) 0
为每个组检索最接近 data.table
的值。
假设以下 DT
:
set.seed(1)
library(data.table)
DT <- data.table(val = rnorm(1000), group = rep(1:10, each = 10)) # 10 groups
我试图将
by = group
和 roll = "nearest"
结合起来,但它只返回最接近的值,而不是按组返回:DT[val == 0, val, by = group, roll = "nearest"]
# group value
#1: 8 0.001105352
我当然可以为每个组重复这个过程,但是随着组数的增加,这将是不切实际的。例如。:
res <- rbind(DT[val == 0 & group = 1, val, by = group, roll = "nearest"],
DT[val == 0 & group = 2, val, by = group, roll = "nearest"],
DT[val == 0 & group = 3, val, by = group, roll = "nearest"],
...)
也许我缺少一些
data.table
功能? 最佳答案
您不一定需要为此加入。
使用 min
和 abs
组合的可能解决方案:
DT[, .(closest.val.to.zero = val[abs(val) == min(abs(val))]), by = group]
这使:
posted by @chinsoon12 in the comments 选项的一种更通用的方式:
DT[CJ(group = group, val = 0, unique = TRUE)
, on = .(group, val)
, .(group, closest.val.to.zero = x.val)
, roll = "nearest"]
关于r - 按组查找最接近的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52363840/