尝试将列表存储在长度为 1 的 data.table 中时出错

Error when trying to store list in data.table of length 1

当试图在 data.table 中存储向量时,这仅在 data.table 的长度大于一时有效。

请在下面找到问题的简化版本

library(data.table)

工作正常

dt <- data.table( a = c("a", "b"), l = list())
dt$l[[1]] <- c(1:3) 

结果:

   a     l
1: a 1,2,3
2: b

生成错误

dt <- data.table( a = c("a"), l = list())
dt$l[[1]] <- c(1:3) 

Error in [<-.data.table(x, j = name, value = value) : Supplied 3 items to be assigned to 1 items of column 'l'. 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.

预期结果:

   a     l
1: a 1,2,3

这是您要找的吗?

dt <- data.table(a = "a", l = list())
dt[1L, l := list(list((1:3)))]

结果:

   a     l
1: a 1,2,3