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

David Winsemius dwinsemius at comcast.net
Thu Feb 3 16:46:33 CET 2011


On Feb 3, 2011, at 10:12 AM, Alastair wrote:

>
> 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,}$")

mixedsort {gtools}
R Documentation
Order or Sort strings with embedded numbers so that the numbers are in  
the correct order
>
> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list