Wolfram Fischer - Z/I/M
2002-Oct-25 08:02 UTC
[R] merge: How to preserve the original order?
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?) Wolfram -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
ripley@stats.ox.ac.uk
2002-Oct-25 12:15 UTC
[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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Maybe Matching Threads
- [PATCH] Btrfs: cleanup for __merge_refs
- patching ?merge to allow the user to keep the order of one of the two data.frame objects merged
- Reshape from long to wide format with date variable
- counting characters starting point
- [LLVMdev] [Debug Info PATCH] for support of ref_addr and removal of DIE duplication