Hi All, I want to "merge" two datasets by column "ID" and I don't want the result to be sorted by "ID". I am doing the following: > z = merge(x, y, by = "ID", sort=F) The result is not sorted by "ID". But (as oppose to what I expected) it is not even in the original order of either "x" or "y". Can somebody tell what to do if I wanted it to be in the original order of x. P.S.: As my dataset is very huge and I couldn't find the right subset of the data which explains the above problem, so I can't attach it at the moment. If anybody knows the answer, please reply; or else I will try to get the right subset. Thanks in advance Utkarsh
Hi there, You can add a order column on x or y and after use this field to order z. Like z<-z[order(z$orderfield),] To generate a order on x or y you can do something like x$xorder<-1:nrow(x) cheers milton On Thu, Dec 24, 2009 at 2:26 PM, <utkarsh.singhal@global-analytics.com>wrote:> > Hi All, > I want to "merge" two datasets by column "ID" and I don't want the result > to > be sorted by "ID". I am doing the following: > > z = merge(x, y, by = "ID", sort=F) > The result is not sorted by "ID". But (as oppose to what I expected) it > is > not even in the original order of either "x" or "y". > Can somebody tell what to do if I wanted it to be in the original order > of > x. > > P.S.: As my dataset is very huge and I couldn't find the right subset of > the > data which explains the above problem, so I can't attach it at the > moment. > If anybody knows the answer, please reply; or else I will try to get the > right subset. > > Thanks in advance > > Utkarsh > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thanks, but I kind of new this way already and it doesn't seem an optimal thing to do. What I was looking for is to pass some argument in 'merge' itself which doesn't change the ordering of 'x'. Or, more than that, I am interested in knowing that why is it changing the order or what order is it taking when I specified sort=F. Regards Utkarsh -------- Original Message -------- Subject: Re: [R] help in merging From: milton ruser <milton.ruser at gmail.com> Date: Fri, December 25, 2009 1:38 am To: utkarsh.singhal at global-analytics.com Cc: r-help at r-project.org Hi there, You can add a order column on x or y and after use this field to order z. Like z<-z[order(z$orderfield),] To generate a order on x or y you can do something like x$xorder<-1:nrow(x) cheers milton On Thu, Dec 24, 2009 at 2:26 PM, <[1]utkarsh.singhal at global-analytics.com> wrote: Hi All, I want to "merge" two datasets by column "ID" and I don't want the result to be sorted by "ID". I am doing the following: > z = merge(x, y, by = "ID", sort=F) The result is not sorted by "ID". But (as oppose to what I expected) it is not even in the original order of either "x" or "y". Can somebody tell what to do if I wanted it to be in the original order of x. P.S.: As my dataset is very huge and I couldn't find the right subset of the data which explains the above problem, so I can't attach it at the moment. If anybody knows the answer, please reply; or else I will try to get the right subset. Thanks in advance Utkarsh ______________________________________________ [2]R-help at r-project.org mailing list [3]https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide [4]http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. References 1. mailto:utkarsh.singhal at global-analytics.com 2. mailto:R-help at r-project.org 3. https://stat.ethz.ch/mailman/listinfo/r-help 4. http://www.r-project.org/posting-guide.html
Not sure if its guaranteed but this sqlite join does seem to preserve the order of the first data frame.> library(sqldf) > BODTime demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8> BODrev <- BOD[6:1,]; BODrevTime demand 6 7 19.8 5 5 15.6 4 4 16.0 3 3 19.0 2 2 10.3 1 1 8.3> sqldf("select * from BODrev, BOD using(Time)")Time demand demand 1 7 19.8 19.8 2 5 15.6 15.6 3 4 16.0 16.0 4 3 19.0 19.0 5 2 10.3 10.3 6 1 8.3 8.3 See home page at: http://sqldf.googlecode.com On Thu, Dec 24, 2009 at 2:26 PM, <utkarsh.singhal at global-analytics.com> wrote:> > ? Hi All, > ? I want to "merge" two datasets by column "ID" and I don't want the result to > ? be sorted by "ID". I am doing the following: > ? > z = merge(x, y, by = "ID", sort=F) > ? The result is not sorted by "ID". But (as oppose to what I expected) it is > ? not even in the original order of either "x" or "y". > ? Can somebody tell what to do if I wanted it to be in the original order of > ? x. > > ? P.S.: As my dataset is very huge and I couldn't find the right subset of the > ? data which explains the above problem, so I can't attach it at the moment. > ? If anybody knows the answer, please reply; or else I will try to get the > ? right subset. > > ? Thanks in advance > > ? Utkarsh > ______________________________________________ > R-help at r-project.org mailing list > 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. >