[R] merge: How to preserve the original order?

Wolfram Fischer wolfram at fischer-zim.ch
Fri Oct 25 16:53:15 CEST 2002


Thank you, Brian and Sundar, for your help!

Indeed, I should have found the first solution myself
by studying thoroughly the help pages.

If there is not a correspondent x.labels$ref for each x.vals$ref
then all.x=T (resp. all.y=T) is necessary. 
But in this case, changing the first and second argument of merge()
not works as expected. If I expand x.vals with 'Ref3' as:

    x.vals <- data.frame(
          id = c( 'A1', 'C2', 'B3', 'D4' )
        , ref = c( 'Ref1', 'Ref2' , 'Ref3','Ref1' )
        , val = c( 1.11, 2.22, 3.33, 4.44 )
        )

And I try:
    merge( x.labels, x.vals, by='ref', all.y = T, sort=F )

I get:
        ref   label id  val
   1   Ref1 Label01 A1 1.11
   2   Ref2 Label02 C2 2.22
   3   Ref1 Label01 D4 4.44
   4   Ref3    <NA> B3 3.33

('Ref3' is now on line 4, but it should be on line 3 as in x.vals.)

This seems to be an exception of what is described on the help page.

Your interesting alternative solution was helpful for this case.
Thanks!

Wolfram


--- In reply to: ---
>Date:    25.10.02 13:15 (+0100)
>From:    ripley at stats.ox.ac.uk
>Subject: Re: [R] merge: How to preserve the original order?
>
> You did get the original order as described in help(merge).  You can't
> have the original order for x *and* the original order for y, and it is
> clearly documented that you get that for y.
> 
> Try
> 
> merge( x.labels, x.vals, by='ref', sort=FALSE )
> 
> after reading the help page.  (Why did you specify all.x=T for
> this example?)
> 
> 
> On Fri, 25 Oct 2002, Wolfram Fischer - Z/I/M wrote:
> 
> > I tried:
> >     x.vals <- data.frame(
> >           id = c( 'A1', 'C2', 'B3' )
> >         , ref = c( 'Ref1', 'Ref2' ,'Ref1' )
> >         , val = c( 1.11, 2.22, 3.33 )
> >         )
> >     x.labels <- data.frame(
> >           ref = c( 'Ref1', 'Ref2' )
> >         , label = c( 'Label01', 'Label02' )
> >         )
> >
> >     merge( x.vals, x.labels, by='ref', all.x = T, sort=F )
> >
> > I received:
> >          ref  id  val   label
> >     1   Ref1  A1 1.11 Label01
> >     2   Ref1  B3 3.33 Label01
> >     3   Ref2  C2 2.22 Label02
> >
> > Alltough 'sort=F' is set, the original order: id = A1, C2, B3 is
> > not preserved. - Is there a possibility to preserve the original
> > order (when there is no key field which can be ordered after merging).
> > (Perhaps 'merge' is not the right solution for this problem?)
> 
> If you have an ordered id field (who would have guessed in this example
> that those were ordered?) you can always sort on it. Here's another
> solution.
> 
> x.vals$order <- seq(len=nrow(x.vals))
> m <- merge( x.vals, x.labels, by='ref', all.x = T, sort=F )
> m[sort.list(m$order), -4]
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272860 (secr)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list