首页 > 代码库 > intersect for multiple vectors in R
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。