[R] Does intersect preserve order?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sun Oct 17 11:49:15 CEST 2021


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



More information about the R-help mailing list