[R] Does intersect preserve order?

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sun Oct 17 11:56:50 CEST 2021


As Duncan notes, it is easy to get the current implementation. Just type
the function name at the prompt:

> intersect
{
     y <- as.vector(y)
    unique(y[match(as.vector(x), y, 0L)])
}

On Sun, Oct 17, 2021 at 12:49 PM Duncan Murdoch <murdoch.duncan using gmail.com>
wrote:

> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list