[R] intersect() without discarding duplicates?
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Fri May 21 01:30:53 CEST 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jonathan
> Sent: Thursday, May 20, 2010 3:25 PM
> To: David Winsemius
> Cc: r-help
> Subject: Re: [R] intersect() without discarding duplicates?
>
> Thanks, but that doesn't quite work, since I'd want the result of b[b
> %in%
> a] to be symmetric with a[a%in%b] (so if there are two 2's in EACH
> vector,
> I'll get two 2's in the result, but if there are two 2's in only one
> vector,
> but one two in the other, the result will show only one 2.
>
> Consider:
>
> > a <- c(2,4,3)
> > b<-c(6,6,5,2,2,8,4)
>
> > b[b %in% a]
> [1] 2 2 4
>
> > a[a%in%b]
> [1] 2 4
>
> The second answer is correct, but I can't predict which variable to put
> in
> which position in the statement, so I'd need them both to be correct.
>
> Best,
> Jonathan
>
I think you have misread the intersect documentation. Given your examples above intersect is what you in fact want.
> a <- c(2,4,3)
> b<-c(6,6,5,2,2,8,4)
> intersect(a,b)
[1] 2 4
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list