码迷,mamicode.com
首页 > 其他好文 > 详细

intersect for multiple vectors in R

时间:2017-06-02 20:27:59      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:more   art   12px   patch   class   filter   war   integer   lis   

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

标签:more   art   12px   patch   class   filter   war   integer   lis   

原文地址:http://www.cnblogs.com/shuaihe/p/6934917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!