组合具有共享值的任何向量
我有一个很大的向量 polyInt
列表,并且希望根据共享值将它们组合起来,删除任何重复的值。例如:
polyInt = list(c(1,2,3,4),
c(4,5,6),
c(7,8,9),
c(9,10,11,12))
会减少为:
combinedVects = list(c(1,2,3,4,5,6),
c(7,8,9,10,11,12))
因为 polyInt
中的前两个和最后两个向量具有共同的值。
我不知道是否有一种方便的方法可以做到这一点。任何帮助将不胜感激!
编辑:这是我的实际列表,而不是简化的示例。某些列表项是单个值,因此这可能会影响解决方案。
vectList = list(c(1,5,6,10,12,20),
c(2,4,10),
3,
c(2,4,21),
c(1,5,9,10,12,20),
c(6,11,34),
c(7,9,10,33),
c(8,17,23),
c(1,5,7,9,12,33),
c(1,2,5,7,10,33),
c(6,11,19),
c(1,5,9,12,13,20,22,24),
c(12,13)
14,
c(15,16,19),
c(15,16,31),
c(8,17,26),
18,
c(11,15,19,25,31,36),
c(1,5,12,20,22),
c(4,21),
c(12,20,22,24),
c(8,23,29,30),
c(12,22,24),
c(19,25),
c(17,26),
27,
c(28,30),
c(23,29),
c(23,28,30,35),
c(16,19,31),
32,
c(7,9,10,33),
c(6,34),
c(30,35),
c(19,36))
I have a large list of vectors polyInt
and would like to combine them based on shared values, removing any duplicate values. For example:
polyInt = list(c(1,2,3,4),
c(4,5,6),
c(7,8,9),
c(9,10,11,12))
Would reduce to:
combinedVects = list(c(1,2,3,4,5,6),
c(7,8,9,10,11,12))
Because the first two and last two vectors in polyInt
have common values.
I'm at a loss as to whether there is a convenient way to do this. Any help would be greatly appreciated!
EDIT: Here is my actual list, not a simplified example. Some of the list items are single values, so this may affect the solution.
vectList = list(c(1,5,6,10,12,20),
c(2,4,10),
3,
c(2,4,21),
c(1,5,9,10,12,20),
c(6,11,34),
c(7,9,10,33),
c(8,17,23),
c(1,5,7,9,12,33),
c(1,2,5,7,10,33),
c(6,11,19),
c(1,5,9,12,13,20,22,24),
c(12,13)
14,
c(15,16,19),
c(15,16,31),
c(8,17,26),
18,
c(11,15,19,25,31,36),
c(1,5,12,20,22),
c(4,21),
c(12,20,22,24),
c(8,23,29,30),
c(12,22,24),
c(19,25),
c(17,26),
27,
c(28,30),
c(23,29),
c(23,28,30,35),
c(16,19,31),
32,
c(7,9,10,33),
c(6,34),
c(30,35),
c(19,36))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在基础 R 中,您可以使用
intersect
和split
:注意:
\
可以替换 lambda 中的function
自 R 4.1 起的类似函数。In base R, you can use
intersect
andsplit
:Note:
\
can replacefunction
in lambda-like functions since R 4.1.