小编use*_*817的帖子

以列表形式返回匹配元素的快速方法

我有两个向量:

set.seed(1)

a <- sample(1:100,200, replace=T)

b <- sample(1:100,40, replace=F)
Run Code Online (Sandbox Code Playgroud)

a我想找到该匹配中元素的位置b:

sapply(b, function(x) which(a %in% x))
Run Code Online (Sandbox Code Playgroud)

这可以完成工作,但需要很长时间

有没有一种方法可以将结果存储在实际上很快的列表中?

所需的输出如下所示:

sapply(b, function(x) which(a %in% x))
[[1]]
integer(0)

[[2]]
integer(0)

[[3]]
[1] 107 142 199

[[4]]
[1] 109 126

[[5]]
[1] 136 167

[[6]]
integer(0)

[[7]]
integer(0)

[[8]]
[1]  73  91 176

[[9]]
[1]  51 146 181
Run Code Online (Sandbox Code Playgroud)

performance r vector

8
推荐指数
2
解决办法
307
查看次数

标签 统计

performance ×1

r ×1

vector ×1