[R] Equivalent of gtools::mixedsort in R base
Sebastien Bihorel
sebastien.bihorel at cognigencorp.com
Mon Mar 12 18:42:38 CET 2018
So I take this is a no to my initial question.
Cheers too.
PS: some users just ask questions to get straight answers not to get a solution to their problem :D
From: "Bert Gunter" <bgunter.4567 at gmail.com>
To: "Sebastien Bihorel" <sebastien.bihorel at cognigencorp.com>
Cc: "R-help" <r-help at r-project.org>
Sent: Monday, March 12, 2018 12:57:00 AM
Subject: Re: [R] Equivalent of gtools::mixedsort in R base
???
> y <- sort( c("a1","a2","a10","a12","a100"))
> y
[1] "a1" "a10" "a100" "a12" "a2"
> mixedsort(y)
[1] "a1" "a2" "a10" "a12" "a100"
**Please read the docs!** They say that mixedsort() and mixedorder() both take a **single vector** as the argument to be sorted or ordered and, as the above indicates, they perform exactly as advertised. **Unlike order()**. So of course your do.call() construction fails.
So presumably you have a data frame with multiple columns of mixed alpha and numerics? (A reproducible example would be most helpful here.)
If this is the case, one **possibly dumb** approach (you have been warned!) would be to turn each column into an ordered factor and then call order() on the data frame of ordered factors via do.call() as above. i.e.
> y1 <- ordered(y,lev = mixedsort(y))
> y1
[1] a1 a10 a100 a12 a2
Levels: a1 < a2 < a10 < a12 < a100
> order(y1)
[1] 1 5 2 4 3
(this is just for 1 vector to show how the idea would work).
Of course, if this is **not** what you want, you'll need to clarify, hopefully with a reprex. Or hope that someone else has better insight than I.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Sun, Mar 11, 2018 at 9:15 PM, Sebastien Bihorel < [ mailto:sebastien.bihorel at cognigencorp.com | 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
______________________________________________
[ mailto:R-help at r-project.org | R-help at r-project.org ] mailing list -- To UNSUBSCRIBE and more, see
[ https://stat.ethz.ch/mailman/listinfo/r-help | https://stat.ethz.ch/mailman/listinfo/r-help ]
PLEASE do read the posting guide [ http://www.r-project.org/posting-guide.html | http://www.R-project.org/posting-guide.html ]
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list