[R] unexpected sort order with merge

Johann Hibschman jhibschman at gmail.com
Thu Apr 7 15:20:19 CEST 2011


B77S <bps0002 at auburn.edu> writes:

> That is odd, I noticed some weird sorting with merge() a while back too and
> always am careful with it now.  Fortunately, sort=FALSE seems to work the
> way one would think most of the time.

Thanks for checking.  Is this on a more recent version of R than 2.10.1?
(I'm half-hoping this is something fixed in a newer R, so I can use it
as an excuse to demand an upgrade.)

> Although, the following results seem weird too!  (adding by="date" makes it
> not sort oddly, regardless of sort=TRUE or FALSE)

[...]

>> merge(d1, d2, by="date", sort=T)
>       date icpn.x foo icpn.y bar
> 1 Jan 2000    500   1    500  10
> 2 Feb 2000    500   2    500  20
> 3 Mar 2000    500   3    500  30
> 4 Apr 2000    500   4    500  40
> 5 May 2000    500   5    500  50
> 6 Jun 2000    500   6    500  60

I think this is equivalent to the "single column" version.  For yearmon
objects, when `merge` only has one column to sort by, it seems to do the
right thing.  It only uses alphabetical order when there are more than
one column.  For ordered factors, though, even the single-column merge
was giving me strange sorts.

-Johann


P.S. Just in case people have bad threading on their mail/news reader, here's
the "bad sort" example:
>> merge(d1, d2)
>       date icpn foo bar
> 1 Apr 2000  500   4  40
> 2 Feb 2000  500   2  20
> 3 Jan 2000  500   1  10
> 4 Jun 2000  500   6  60
> 5 Mar 2000  500   3  30
> 6 May 2000  500   5  50



More information about the R-help mailing list