Re: [R] sorting variable names containing digits

2008-12-22 Thread Gabor Grothendieck
Note that mysort2 is slightly more general as it handles the case that the strings begin with numerics: > u <- c("51a2", "2a4") > mysort(u) [1] "51a2" "2a4" > mysort2(u) [1] "2a4" "51a2" On Mon, Dec 22, 2008 at 12:32 AM, John Fox wrote: > Dear Gabor, > > Thank you (again) for this second sugges

Re: [R] sorting variable names containing digits

2008-12-21 Thread John Fox
Dear Gabor, Thank you (again) for this second suggestion, which does exactly what I want. At the risk of appearing ungrateful, and although the judgment is admittedly subjective, I don't find it simpler than mysort(). For curiosity, I tried some timings of the two functions for the sample problem

Re: [R] sorting variable names containing digits

2008-12-21 Thread Gabor Grothendieck
Another possibility is to use strapply in gsubfn giving a solution that is non-recursive and shorter: library(gsubfn) mysort2 <- function(s) { L <- strapply(s, "([0-9]+)|([^0-9]+)", ~ if (nchar(x)) sprintf("%9d", as.numeric(x)) else y) L2 <- t(do.call(cbind, lapply

Re: [R] sorting variable names containing digits

2008-12-21 Thread John Fox
Dear Gabor, Thanks for this -- I was unaware of mixedsort(). As you point out, however, mixedsort() doesn't cover all of the cases in which I'm interested and which are handled by mysort(). Regards, John On Sun, 21 Dec 2008 20:51:17 -0500 "Gabor Grothendieck" wrote: > mixedsort in gtools will

Re: [R] sorting variable names containing digits

2008-12-21 Thread Gabor Grothendieck
mixedsort in gtools will give the same result as mysort(s) but differs in the case of t. On Sun, Dec 21, 2008 at 8:33 PM, John Fox wrote: > Dear r-helpers, > > I'm looking for a way of sorting variable names in a "natural" order, when > the names are composed of digits and other characters. I kno

[R] sorting variable names containing digits

2008-12-21 Thread John Fox
Dear r-helpers, I'm looking for a way of sorting variable names in a "natural" order, when the names are composed of digits and other characters. I know that this is a vague idea, and that sorting character strings is a complex topic, but perhaps a couple of examples will clarify what I mean: > s