[R] How do I tell whether two vectors are identical?

David Winsemius dwinsemius at comcast.net
Tue Oct 12 22:37:55 CEST 2010


On Oct 12, 2010, at 1:58 PM, David Winsemius wrote:

> And ...
>
> ?which
>
> which(abs(x-y) >0)   # or if these are subject to FAQ 7.31, then
> which( zapsmall( abs(x-y)) > 0 )
>
> .. for second part of question.

THe help page answer would be along these lines:

if ( isTRUE(all.equal( x, y)) {"equal"} else{
        all.equal( x, y)}

My zapsmall method is not robust to differences in length or NA  
entries, but those can be added to the test criteria. (And it seems  
possible that other gotcha's are lurking in the weeds.)

The method of embedding in a dataframe is going to be really slow.

require(rbenchmark)
benchmark(
   all_eq = {isTRUE(all.equal.numeric(x,y))},   # not much faster than  
just all.equal
   zapper = {all( c(length(x) == length(y),
                   is.na(x)==is.na(y),
                   zapsmall(na.omit(x)-na.omit(y)) == 0 )
                  )},
   dfrm = {compare<-data.frame(1:length(x),x,y);
          differences<-compare$id[x!=y];
          is.null(differences)},
   replications=1000)

     test replications elapsed  relative user.self sys.self user.child  
sys.child
1 all_eq         1000   0.233  3.328571     0.233    0.001           
0         0
3   dfrm         1000   0.771 11.014286     0.771    0.002           
0         0
2 zapper         1000   0.070  1.000000     0.070    0.001           
0         0

And for most real applications, isTRUE(all.equal(x,y)) is going to be  
the way to go.

>
> -- David.
> On Oct 12, 2010, at 1:43 PM, Bert Gunter wrote:
>
>> Try preusing an Intro to R for such elementary questions, please.  
>> Also
>> the R Help facility.
>>
>> But...
>>
>> ?identical   ##(oddly enough...)
>> ?"=="
>>
>> But beware numerical issues -- see R FAQ 7.31.
>>
>> -- Bert
>>
>>
>> On Tue, Oct 12, 2010 at 10:35 AM, ANJAN PURKAYASTHA
>> <anjan.purkayastha at gmail.com> wrote:
>>> Hi,
>>> I have two vectors, each of length 45000.
>>> How do I compare the vectors to ascertain if they are identical.  
>>> Secondly if
>>> they are NOT identical, how do I determine the indices of  
>>> positions at which
>>> the vectors differ?
>>> Thanks,
>>> Anjan
>>>
>>>
>>> --
>>> ===================================
>>> anjan purkayastha, phd.
>>> research associate
>>> fas center for systems biology,
>>> harvard university
>>> 52 oxford street
>>> cambridge ma 02138
>>> phone-703.740.6939
>>> ===================================
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> -- 
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> ______________________________________________
>> 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
>
> ______________________________________________
> 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