R sorts a vector on its own accord

In response to Dwin's comment on Dirk's answer: the data are always putty in your hands. "This is R. There is no if. Only how." -- Simon Blomberg

You can add 0 like so:

df.sorted <- gsub("(walker)([[:digit:]]{1}_)", "\\10\\2", df.sorted)

If you needed to add 00, you do it like this:

df.sorted <- gsub("(walker)([[:digit:]]{1}_)", "\\10\\2", df.sorted)
df.sorted <- gsub("(walker)([[:digit:]]{2}_)", "\\10\\2", df.sorted)

...and so on.


construct it as an ordered factor:

> df.new <- ordered(df.sorted,levels=df.sorted)
> order(df.new)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 ...

EDIT :

After @DWins comment, I want to add that it is even not nessecary to make it an ordered factor, just a factor is enough if you give the right order of levels :

>     df.new2 <- factor(df.sorted,levels=df.sorted)
>     order(df.new)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 ...

The difference will be noticeable when you use those factors in a regression analysis, they can be treated differently. The advantage of ordered factors is that they let you use comparison operators as < and >. This makes life sometimes a lot easier.

> df.new2[5] < df.new2[10]
[1] NA
Warning message:
In Ops.factor(df.new[5], df.new[10]) : < not meaningful for factors

> df.new[5] < df.new[10]
[1] TRUE

Use the mixedsort (or) mixedorder functions in package gtools:

require(gtools)
mixedorder(df.sorted)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
[28] 28 29 30 31 32 33 34 35 36 37 38 39

Isn't this simply the same thing you get with all lexicographic shorts (as e.g. ls on directories) where walker10_foo sorts higher than walker1_foo?

The easiest way around, in my book, is to use a consistent number of digits, i.e. I would change to binned_walker01_1.grd and so on inserting a 0 for the one-digit counts.

Tags:

Sorting

R

Vector