我试图在导入到R中的data.table对象中找到两个列表列之间的交集。我复制了下面的data.table,并使用完全相同的值:
DT_1 <- data.table(
ego = as.integer(c(128320, 128320)),
list_ego = list(as.integer(c(1,4)), as.integer(c(1,4))),
alter = as.integer(c(48259, 167757)),
list_alter = list(as.integer(c(4,3,1,5)), as.integer(c(3,1,4,5)))
)
然后我运行下面的代码并得到一条错误消息:
> DT_1[, shared_list := mapply(FUN = intersect, list_ego, list_alter)]
Error in `[.data.table`(DT_1, , `:=`(shared_list, mapply(FUN = intersect, :
Supplied 4 items to be assigned to 2 items of column 'shared_list'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.
In addition: Warning message:
In `[.data.table`(DT_1, , `:=`(shared_list, mapply(FUN = intersect, :
2 column matrix RHS of := will be treated as one vector
奇怪的是,当我使用其他值时,同样的代码也能工作:
> DF_2 <- data.table(
+ ego = as.integer(c(1, 1)),
+ list_ego = list(as.integer(c(100,200)), as.integer(c(100,200))),
+ alter = as.integer(c(2, 3)),
+ list_alter = list(as.integer(c(100, 300)), as.integer(c(200, 300)))
+ )
> DF_2[, shared_list := mapply(FUN = intersect, list_ego, list_alter)]
> DF_2
ego list_ego alter list_alter shared_list
1: 1 100,200 2 100,300 100
2: 1 100,200 3 200,300 200
我需要让这段代码适用于所有值,因为我将在许多csv导入的data.table对象的循环中使用它。
1条答案
按热度按时间ubbxdtey1#
提示在错误消息中。请使用
SIMPLIFY = FALSE
,尽可能避免将结果转换为矩阵:您也可以使用
Map()
: