[R] Equivalent of gtools::mixedsort in R base

Sebastien Bihorel sebastien.bihorel at cognigencorp.com
Tue Mar 13 13:58:55 CET 2018


Thanks.

----- Original Message -----
From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
To: "Sebastien Bihorel" <sebastien.bihorel at cognigencorp.com>
Cc: r-help at r-project.org
Sent: Monday, March 12, 2018 3:49:10 PM
Subject: Re: [R] Equivalent of gtools::mixedsort in R base

split any mixed columns into letter and number columns
and then order can be used on that:

  DF <- data.frame(x = c("a10", "a2", "a1"))
  o <- do.call("order", transform(DF, let = gsub("\\d", "", x),
                                                         no =
as.numeric(gsub("\\D", "", x)),
                                                         x = NULL))
  DF[o,, drop = FALSE ]


On Mon, Mar 12, 2018 at 12:15 AM, Sebastien Bihorel
<sebastien.bihorel at cognigencorp.com> wrote:
> Hi,
>
> Searching for functions that would order strings that mix characters and numbers in a "natural" way (ie, "a1 a2 a10" instead of "a1 a10 a2"), I found the mixedsort and mixedorder from the gtools package.
>
> Problems:
> 1- mixedorder does not work in a "do.call(mixedorder, mydataframe)" call like the order function does
> 2- gtools has not been updated in 2.5 years
>
> Are you aware of an equivalent of this function in base R or a another contributed package (with correction of problem #1)?
>
> Thanks
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list