R中列表的交集 [英] Intersection of lists in R

查看:64
本文介绍了R中列表的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个接收列表 x 并返回列表 y 的函数,使得 y [[i]] = intersect(x [[1]] [[i]],x [[2]] [[i]],...)吗?

Is there a function that receives a list x and returns a list y such that y[[i]] = intersect(x[[1]][[i]], x[[2]][[i]], ...) ?

如果没有,是否有R方法可以在几行中进行编码?

If not, is there a R way to code it in a couple of lines?

推荐答案

这项工作有效吗?

x <- list(list(1:3,2:4),list(2:3,4:5),list(3:7,4:5))
maxlen <- max(sapply(x,length))
lapply(seq(maxlen),function(i) Reduce(intersect,lapply(x,"[[",i)))

( intersect 仅接受两个参数,因此您必须使用 Reduce 作为附加步骤)

(intersect only takes two arguments so you have to use Reduce as an additional step)

PS我没有在任何困难的情况下尝试过此操作-例如长度不均的列表.

PS I haven't tried this on any hard cases -- e.g. lists of uneven length.

这篇关于R中列表的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆