比较R中的多维列表

时间:2013-09-09 18:03:16

标签: r vectorization

我有以下两个清单:

First list:
[[1]]
[1] "ab" "iew"  "rer" "fdd"     
[[2]]
[1] "ff" "de 
[[3]]
[1] "cc"

Second list:
[[1]]
[1] "iew" "vfr" 
[[2]]
[1] "ff" "cdc"
[[3]]
[1] "vf" "cde"

我的目标是比较这两个多维列表,结果将是:

[[1]]
[1] FALSE TRUE FALSE FALSE 
[[2]]
[1] TRUE FALSE
[[3]]
[1] FALSE

预先形成此intersect()函数的最佳矢量化方法是什么?

1 个答案:

答案 0 :(得分:5)

以下是使用mapply

的替代方法
> mapply("%in%", Firt.list, Second.list)
[[1]]
[1] FALSE  TRUE FALSE FALSE

[[2]]
[1]  TRUE FALSE

[[3]]
[1] FALSE

First.listSecond.list的位置:

Firt.list <- list(c("ab", "iew",  "rer", "fdd" ), c("ff", "de"), c("cc"))
Second.list <- list(c("iew", "vfr"), c("ff", "cdc"), c("vf", "cde"))

如果您想知道哪些值是列表的intersect,请尝试使用

> mapply("intersect", Firt.list, Second.list)
[[1]]
[1] "iew"

[[2]]
[1] "ff"

[[3]]
character(0)