这与可以在以下位置找到的先前问题有关:
下面是数据:
DT <- data.table(a = sample(c("C","M","Y","K"), 100, rep=TRUE),
b = sample(c("A","S"), 100, rep=TRUE),
f = round(rnorm(n=100, mean=.90, sd=.08),digits = 2) ); DT
我想对以下功能进行优雅而简洁的重写:
`%between%` <- function(x, vals) { x >= vals[1] & x <= vals[2]}
`%nbetween%` <- Negate(`%between%`)
以及以下脚本将满足某些条件的某些值替换为 NA
DT[a == "C" & b %in% c("A", "S") & f %nbetween% c(.85, .95), f := NA]
DT[a == "M" & b %in% c("A", "S") & f %nbetween% c(.85, .95), f := NA]
DT[a == "Y" & b %in% c("A", "S") & f %nbetween% c(.80, .90), f := NA]
DT[a == "K" & b %in% c("A", "S") & f %nbetween% c(.95, 1.10), f := NA]