[R] order() not producing results as I expect

S Ellison S.Ellison at lgcgroup.com
Fri Oct 11 17:19:13 CEST 2013

> -----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}}

More information about the R-help mailing list