Re: [R] Sorting alphanumerically

From: Marc Schwartz (via MN) <>
Date: Sat 25 Feb 2006 - 06:10:17 EST

On Fri, 2006-02-24 at 12:54 -0600, mtb954 mtb954 wrote:
> I'm trying to sort a DATAFRAME by a column "ID" that contains
> alphanumeric data. Specifically,"ID" contains integers all preceeded
> by the character "g" as in:
> g1, g6, g3, g19, g100, g2, g39
> I am using the following code:
> and was hoping it would sort the dataframe by ID in the following manner
> g1, g2, g3, g6, g19, g39, g100
> but it doesn't sort at all. Could anyone point out my mistake?
> Thank you.
> Mark

The values are being sorted by character based ordering, which may be impacted upon by your locale.

Thus, on my system, you end up with something like the following:

> ID[order(ID)]

[1] "g1" "g100" "g19" "g2" "g3" "g39" "g6"

What you can do, based upon the presumption that the prefix of 'g' is present as you describe above, is:

> ID[order(as.numeric((gsub("g", "", ID))))]
[1] "g1" "g2" "g3" "g6" "g19" "g39" "g100"

What this does is to use gsub() to strip the 'g' and then order by numeric value.

HTH, Marc Schwartz mailing list PLEASE do read the posting guide! Received on Sat Feb 25 06:24:56 2006

This archive was generated by hypermail 2.1.8 : Sat 25 Feb 2006 - 09:08:39 EST