R语言 查找列表交集(使用mapply)的代码适用于相同的值,但不适用于其他值

o7jaxewo  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(184)

我试图在导入到R中的data.table对象中找到两个列表列之间的交集。我复制了下面的data.table,并使用完全相同的值:

  1. DT_1 <- data.table(
  2. ego = as.integer(c(128320, 128320)),
  3. list_ego = list(as.integer(c(1,4)), as.integer(c(1,4))),
  4. alter = as.integer(c(48259, 167757)),
  5. list_alter = list(as.integer(c(4,3,1,5)), as.integer(c(3,1,4,5)))
  6. )

然后我运行下面的代码并得到一条错误消息:

  1. > DT_1[, shared_list := mapply(FUN = intersect, list_ego, list_alter)]
  2. Error in `[.data.table`(DT_1, , `:=`(shared_list, mapply(FUN = intersect, :
  3. 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.
  4. In addition: Warning message:
  5. In `[.data.table`(DT_1, , `:=`(shared_list, mapply(FUN = intersect, :
  6. 2 column matrix RHS of := will be treated as one vector

奇怪的是,当我使用其他值时,同样的代码也能工作:

  1. > DF_2 <- data.table(
  2. + ego = as.integer(c(1, 1)),
  3. + list_ego = list(as.integer(c(100,200)), as.integer(c(100,200))),
  4. + alter = as.integer(c(2, 3)),
  5. + list_alter = list(as.integer(c(100, 300)), as.integer(c(200, 300)))
  6. + )
  7. > DF_2[, shared_list := mapply(FUN = intersect, list_ego, list_alter)]
  8. > DF_2
  9. ego list_ego alter list_alter shared_list
  10. 1: 1 100,200 2 100,300 100
  11. 2: 1 100,200 3 200,300 200

我需要让这段代码适用于所有值,因为我将在许多csv导入的data.table对象的循环中使用它。

ubbxdtey

ubbxdtey1#

提示在错误消息中。请使用SIMPLIFY = FALSE,尽可能避免将结果转换为矩阵:

  1. DT_1[, shared_list := mapply(FUN = intersect, list_ego, list_alter, SIMPLIFY = FALSE)]
  2. # ego list_ego alter list_alter shared_list
  3. # <int> <list> <int> <list> <list>
  4. # 1: 128320 1,4 48259 4,3,1,5 1,4
  5. # 2: 128320 1,4 167757 3,1,4,5 1,4

您也可以使用Map()

  1. DT_1[, shared_list := Map(f = intersect, list_ego, list_alter)]

相关问题