可以在两行中获得所需的行为,但是一行失败(参见下面的代码)。怀疑与j中的temp变量有关,或者在fail-1中使用by和j中的列,尽管fail-2也提供了NA。
MWE和输出(首先是所需的)如下所示:
library(data.table)
foostr <- "ab"
foodt <- data.table(v1 = letters[1:3], v2 = 1:3)
# desired output
bardt <- foodt[, .(v3 = sapply(.SD, grepl, x = foostr)), by = c("v1", "v2"), .SDcols = "v1"]
bardt[, v4 := .SD$v2*.SD$v3, by = "v1"]
print(bardt)
# v1 v2 v3 v4
# 1: a 1 TRUE 1
# 2: b 2 TRUE 2
# 3: c 3 FALSE 0
# fail-1: NAs?
bardt <- foodt[, .(v3 = {foov3 = sapply(.SD, grepl, x = foostr)}, v4 = .SD$v2*foov3), by = c("v1", "v2"), .SDcols = "v1"]
print(bardt)
# v1 v2 v3 v4
# 1: a 1 TRUE NA
# 2: b 2 TRUE NA
# 3: c 3 FALSE NA
# fail-2: NAs?
bardt <- foodt[, .(v3 = {foov3 = sapply(.SD, grepl, x = foostr)}, v4 = .SD$v2*foov3), by = c("v1"), .SDcols = "v1"]
print(bardt)
# v1 v3 v4
# 1: a TRUE NA
# 2: b TRUE NA
# 3: c FALSE NA