繁体   English   中英

R中列表中的向量和多个向量之间的匹配

[英]Matching between a vector and multiple vectors in a list in R

我有一个矢量列表,如:

>list

[[1]]

[1] "a" "m" "l" "s" "t" "o"

[[2]]

[1] "a" "y" "o" "t" "e"

[[3]]

[1] "n" "a" "s" "i" "d"

我想找到他们每个人和剩下的人之间的匹配(即在第一个和另外两个之间,第二个和另外两个,依此类推),并保持夫妇匹配次数最多。 我可以通过“for”循环和夫妻相交来做到这一点。 例如

for (i in 2:3) { intersect(list[[1]],list[[i]]) }

然后将输出保存到矢量或其他结构中。 然而,这对我来说似乎效率低下(给出的不是3而不是3)我想知道R是否有一些内置函数以巧妙的方式做到这一点。

所以问题是:

有没有办法在没有明确使用“for”循环的情况下查找一个向量与向量列表的匹配?

我不相信这有内置功能。 你可以尝试的最好的是:

lsts <- lapply(1:5, function(x) sample(letters, 10)) # make some data (see below)
maxcomb <- which.max(apply(combs <- combn(length(lsts), 2), 2,
  function(ix) length(intersect(lsts[[ix[1]]], lsts[[ix[2]]]))))
lsts <- lsts[combs[, maxcomb]]
# [[1]]
#  [1] "m" "v" "x" "d" "a" "g" "r" "b" "s" "t"

# [[2]]
#  [1] "w" "v" "t" "i" "d" "p" "l" "e" "s" "x"

转储原件:

[[1]]
 [1] "z" "r" "j" "h" "e" "m" "w" "u" "q" "f"

[[2]]
 [1] "m" "v" "x" "d" "a" "g" "r" "b" "s" "t"

[[3]]
 [1] "w" "v" "t" "i" "d" "p" "l" "e" "s" "x"

[[4]]
 [1] "c" "o" "t" "j" "d" "g" "u" "k" "w" "h"

[[5]]
 [1] "f" "g" "q" "y" "d" "e" "n" "s" "w" "i"
datal <- list (a=c(2,2,1,2),
           b=c(2,2,2,4,3),
           c=c(1,2,3,4))

# all possible combinations
combs <- combn(length(datal), 2)
# split into list
combs <- split(combs, rep(1:ncol(combs), each = nrow(combs)))

# calculate length of intersection for every combination
intersections_length <- sapply(combs, function(y) {
  length(intersect(datal[[y[1]]],datal[[y[2]]]))
  }
  )

# What lists have biggest intersection
combs[which(intersections_length == max(intersections_length))]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM