[R] intersect() without discarding duplicates?
David Winsemius
dwinsemius at comcast.net
Fri May 21 03:01:20 CEST 2010
On May 20, 2010, at 7:30 PM, Nordlund, Dan (DSHS/RDA) wrote:
>> -----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
He wants this result.
> a <- c(2,4,2,3)
> b<-c(6,6,5,2,2,8,4)
> somefn(a,b)
[1] 2 4 2
And this result:
> a <- c(2,4,3)
> b<-c(6,6,5,2,2,8,4)
> somefn(a,b)
[1] 2 4
>somefn(b,a)
[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
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list