[R] Does intersect preserve order?

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Mon Oct 18 00:59:23 CEST 2021


intersect() is a generic function so the question is which one does someone
want to know if it remains in the same order?

But a deeper question is what ORDER? 

intersect(A, B)
intersect(B, A)

Note the results have to be the same but not the order unless they start
sorted the same way.

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Duncan Murdoch
Sent: Sunday, October 17, 2021 5:49 AM
To: petr smirnov <psmirnov2000 using gmail.com>; r-help using r-project.org
Subject: Re: [R] Does intersect preserve order?

On 15/10/2021 4:31 p.m., petr smirnov wrote:
> Hi,
> 
> Is base::intersect guaranteed to return items in the order they 
> (first) appear in the first argument? I couldn't find any mention of 
> this in the help file for set operations.

No, that's just what the current implementation does.

It's conceivable that swapping x and y could let it be faster in some
circumstances.  Or maybe there's a completely different implementation
that's better for some data types.  In either of those cases the order could
change.

Generally speaking, the functions that treat vectors as sets make no
assumptions and no guarantees about order, because sets are unordered.

If you need the current behaviour to be guaranteed, probably the easiest way
is to copy the function:  it's very simple.

Duncan Murdoch

> 
> If so, could this be documented on the help page?
> 
> Thanks,
> Petr

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list