Hello, I'm using R version 3.0.0 on a mac. I'm having trouble getting order to behave as I expect it should. I'm trying to sort a data.frame according to a character vector. I'm able to sort the data.frame, but it retruns an unexpected result. I have no idea where the order that is being produced comes from. Any ideas on how to properly order a data frame by a character vector? Here is the current order of the data frame (called str.dat):> head(str.dat)str.names POPINFO POPFLAG LOCDATA Loc1 Loc2 Loc3 ind.names 1 alba1.pop3 3 0 1 12 3 2 alba1 2 alba2.pop3 3 0 1 3 3 2 alba2 3 alch1.pop4 4 0 2 2 3 2 alch1 4 alch2.pop4 4 0 2 2 3 2 alch2 5 alco1.pop4 4 0 3 3 3 2 alco1 6 alco2.pop4 4 0 3 3 3 2 alco2 Here's the order I expect it to be in when I use order:> head(data.frame(gen.names))gen.names 1 magv1 2 magv2 3 magv3 4 magv4 5 lc1 6 lc2 Here's the order I'm getting:> head(str.dat[order(gen.names),])str.names POPINFO POPFLAG LOCDATA Loc1 Loc2 Loc3 ind.names 111 ncle2.pop5 5 0 39 3 3 2 ncle2 112 ncle3.pop5 5 0 39 2 2 2 ncle3 146 wvma1.pop8 8 0 57 3 3 2 wvma1 145 wvfa2.pop8 8 0 56 3 3 2 wvfa2 55 flse6.pop2 2 0 19 2 5 4 flse6 54 flse5.pop2 2 0 19 2 5 4 flse5 Many thanks, Karl [[alternative HTML version deleted]]
On 10/10/2013 12:00 PM, Karl Fetter wrote:> Hello, > > I'm using R version 3.0.0 on a mac. I'm having trouble getting order to > behave as I expect it should. I'm trying to sort a data.frame according to > a character vector. I'm able to sort the data.frame, but it retruns an > unexpected result. I have no idea where the order that is being produced > comes from.It comes from gen.names, which is not a column of your dataset. I think you'll have to give us something reproducible before we can help you. Duncan Murdoch> > Any ideas on how to properly order a data frame by a character vector? > > Here is the current order of the data frame (called str.dat): > > > head(str.dat) > str.names POPINFO POPFLAG LOCDATA Loc1 Loc2 Loc3 ind.names > 1 alba1.pop3 3 0 1 12 3 2 alba1 > 2 alba2.pop3 3 0 1 3 3 2 alba2 > 3 alch1.pop4 4 0 2 2 3 2 alch1 > 4 alch2.pop4 4 0 2 2 3 2 alch2 > 5 alco1.pop4 4 0 3 3 3 2 alco1 > 6 alco2.pop4 4 0 3 3 3 2 alco2 > > > > Here's the order I expect it to be in when I use order: > > > head(data.frame(gen.names)) > gen.names > 1 magv1 > 2 magv2 > 3 magv3 > 4 magv4 > 5 lc1 > 6 lc2 > > > Here's the order I'm getting: > > > head(str.dat[order(gen.names),]) > str.names POPINFO POPFLAG LOCDATA Loc1 Loc2 Loc3 ind.names > 111 ncle2.pop5 5 0 39 3 3 2 ncle2 > 112 ncle3.pop5 5 0 39 2 2 2 ncle3 > 146 wvma1.pop8 8 0 57 3 3 2 wvma1 > 145 wvfa2.pop8 8 0 56 3 3 2 wvfa2 > 55 flse6.pop2 2 0 19 2 5 4 flse6 > 54 flse5.pop2 2 0 19 2 5 4 flse5 > > > > > Many thanks, > > Karl > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
> -----Original Message----- > I'm using R version 3.0.0 on a mac. I'm having trouble getting order to > behave as I expect it should. I'm trying to sort a data.frame according > to a character vector. I'm able to sort the data.frame, but it retruns an > unexpected result. I have no idea where the order that is being > produced comes from.You've asked for the data frame in the order that would put gen.names in increasing value order. But the order you expect gen.names to be in after ordering is not in increasing-value order. Assuming no missing row numbers in str.dat, it looks like gen.names[111] is the first value in the order you have asked for, gen.names[112] the second (or a tie), gen.names[146] the third and so on. So it sounds to me as if you're asking R for something you didn't want (not uncommon, btw). And with no visible relation between the values in gen.names in your email and the values in str.dat, I'm afraid I can't see why there should be any relationship at all, never mind why you aren't getting it. As an aside, when you asked for order(gen.names), you do realise that you have asked for the order of the whole data frame and not just the vector gen.names$gen.names, yes? In this case that's equivalent as you have a single-column data frame, but in general that might not be a very good idea. S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}