intersect for multiple vectors in R

Posted 何帅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了intersect for multiple vectors in R相关的知识,希望对你有一定的参考价值。

Say you have

a <- c(1,3,5,7,9)
b <- c(3,6,8,9,10)
c <- c(2,3,4,5,7,9)

A straightforward way to do the job is:

intersect(intersect(a,b),c)

More cleverly, and more conveniently if you have a lot of arguments:

Reduce(intersect, list(a,b,c))

The Reduce function is part of funprog {base}, which includes

Reduce(f, x, init, right = FALSE, accumulate = FALSE)
Filter(f, x)
Find(f, x, right = FALSE, nomatch = NULL)
Map(f, ...)
Negate(f)
Position(f, x, right = FALSE, nomatch = NA_integer_)

The other solution like this

intersectSeveral <- function(...) { Reduce(intersect, list(...)) } 
intersectSeveral(a, b, c)

  

以上是关于intersect for multiple vectors in R的主要内容,如果未能解决你的问题,请参考以下文章

Python Set intersection() 方法

[LeetCode][Python]Intersection of Two Arrays

Python 解LeetCode:Intersection of Two Arrays

[LeetCode][Python]Intersection of Two Arrays II

python intersection.py

[LeetCode&Python] Problem 350. Intersection of Two Arrays II