在data.table
的分组命令中,每个组可以返回多行吗?在dplyr
中,这是通过reframe
完成的:
y <- c("a", "b", "d", "f")
df <- tibble(
g = c(1, 1, 1, 2, 2, 2, 2),
x = c("e", "a", "b", "e", "f", "c", "a")
)
library(dplyr)
df %>%
reframe(x = setdiff(x, y), .by = g)
# g x
# 1 e
# 2 e
# 2 c
在data.table
中,这将返回错误:
library(data.table)
dt <- setDT(df)
dt[, x := setdiff(x, y), g]
Error in
[.data.table
(df, ,:=
(x, intersect(x, y)), g) :
Supplied 2 items to be assigned to group 1 of size 3 in column 'x'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
不管怎么说,要拿到相当于reframe
分的data.table
分?