R中列表的交集

编程入门 行业动态 更新时间:2024-10-15 08:22:07
本文介绍了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中列表的交集

本文发布于:2023-11-29 12:25:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!