Skip to content
Prev 24610 / 398502 Next

merge: How to preserve the original order?

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