[R] Sorting a Data Frame by hybrid string / number key

Alastair alastair.andrew at gmail.com
Thu Feb 3 16:12:43 CET 2011


Hi,

I'm trying to present a table of some experimental data, and I want to order
the rows by the instance names. The issue I've got is that there are a
variety of conventions for the instance names (e.g. competition01,
competition13, small_1, big_20, med_9). What I want to be able to sort them
first in category order so: competition < small < med < big, and then
perform the secondary ordering by the final one or two digits. 

I've used Hadley Wickham's StringR package to split the names into the
string and numeric sections so I could get those ordered easily enough. What
I'm struggling with is how to sort the categories (because I don't want them
in a straight alphabetic order).

library(stringr)
d <- data.frame(instance =
c("competition11","competition01","big_20","small_4","small_2","med_9"))
id <- str_extract(d$instance, "\\d{1,}$")

Any pointers would be gratefully received. 
Thanks,
Alastair

-- 
View this message in context: http://r.789695.n4.nabble.com/Sorting-a-Data-Frame-by-hybrid-string-number-key-tp3258283p3258283.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list