Ken Termiso
2005-May-05 20:14 UTC
[R] Intersection of more than two groups in one function?
Hi all, As far as I can tell, the only canned way to do an intersect between two vectors of ints is the intersect(vec1, vec2) function -- is there another function I'm missing for intersecting more than two vectors?? TIA, Ken
Liaw, Andy
2005-May-05 20:21 UTC
[R] Intersection of more than two groups in one function?
Here's a brute-force approach:> "%i%" <- intersect > x1 <- c(1, 3, 4, 7) > x2 <- c(3, 7, 8) > x3 <- c(7, 1) > x1 %i% x2 %i% x3[1] 7 Andy> From: Ken Termiso > > Hi all, > > As far as I can tell, the only canned way to do an intersect > between two > vectors of ints is the intersect(vec1, vec2) function -- is > there another > function I'm missing for intersecting more than two vectors?? > > TIA, > Ken > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Berton Gunter
2005-May-05 21:05 UTC
[R] Intersection of more than two groups in one function?
No, I know of no "canned" way to do it, as it's based on match(), which is for two vectors only. A brute force loop to do it is simple, as I suspect you realize. Perhaps a faster way is to use tabulate() to find values that appear n times (for n vectors). That is: ## assume your integer vectors are components of a list, mylist. ## warning: not tested bigvec<-unlist(mylist) sort(unique(bigvec))[tabulate(bigvec)==length(mylist)] -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ken Termiso > Sent: Thursday, May 05, 2005 1:15 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Intersection of more than two groups in one function? > > Hi all, > > As far as I can tell, the only canned way to do an intersect > between two > vectors of ints is the intersect(vec1, vec2) function -- is > there another > function I'm missing for intersecting more than two vectors?? > > TIA, > Ken > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >