0

I have a data frame like this:

mydf <- data.frame(A=paste(sample(LETTERS, 4), sample(1:20, 20), sep="-"),
        B=paste(sample(1:20, 20), sample(LETTERS, 4), sep="-"),
        C=sample(LETTERS, 20), D=sample(1:100, 20), value=rnorm(20))
> mydf
      A    B C  D       value
1   P-2 15-D F 99  1.46364510
2   N-1 10-O M 96 -0.23821853
3   V-5 19-K N  5  0.09029719
4  Y-15 13-H I 97 -0.59500333
5  P-10 12-D B 14 -0.66379935
6  N-13 20-O K 86  0.18863555
7  V-12  2-K Y 18  0.30263287
8  Y-20 16-H D 89  0.61308283
9   P-8  1-D S 78  1.15020150
10  N-4  3-O L 72 -0.50010804
11 V-11  9-K J 94 -0.84123257
12  Y-9  6-H U 43  1.27020654
13  P-6  7-D V  8  1.88239736
14  N-3 17-O O 40 -0.12517285
15  V-7 11-K A 53  1.30788389
16 Y-17  4-H C 44 -0.48888627
17 P-16  5-D Q 71 -0.08965281
18 N-19  8-O G 66  2.68713761
19 V-14 14-K R 88  0.02622117
20 Y-18 18-H T 80 -2.93330039

Now I know how to order it according to multiple columns, but not how to do it in a NATURAL fashion...

mydf2 <- mydf[order(mydf[,1], mydf[,2], mydf[,3], decreasing=FALSE),]
> mydf2
      A    B C  D       value
2   N-1 10-O M 96 -0.23821853
6  N-13 20-O K 86  0.18863555
18 N-19  8-O G 66  2.68713761
14  N-3 17-O O 40 -0.12517285
10  N-4  3-O L 72 -0.50010804
5  P-10 12-D B 14 -0.66379935
17 P-16  5-D Q 71 -0.08965281
1   P-2 15-D F 99  1.46364510
13  P-6  7-D V  8  1.88239736
9   P-8  1-D S 78  1.15020150
11 V-11  9-K J 94 -0.84123257
7  V-12  2-K Y 18  0.30263287
19 V-14 14-K R 88  0.02622117
3   V-5 19-K N  5  0.09029719
15  V-7 11-K A 53  1.30788389
4  Y-15 13-H I 97 -0.59500333
16 Y-17  4-H C 44 -0.48888627
20 Y-18 18-H T 80 -2.93330039
8  Y-20 16-H D 89  0.61308283
12  Y-9  6-H U 43  1.27020654

You see in the example above that I get the order N-1, N-13, N-19, N-3, N-4, when the correct natural order should be N-1, N-3, N-4, N-13, N-19. How can I do the same I do, but applying a natural sorting?

Is there a mixedorder that works the same as order, like there is a mixedsort and a sort?

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
DaniCee
  • 2,397
  • 6
  • 36
  • 59
  • Try `mydf[do.call(order, c(mydf[1:3], list(decreasing = TRUE))),]` – akrun Jan 04 '19 at 05:31
  • That does the same as my code, but with `decreasing=TRUE` instead of `FALSE`, I need NATURAL sorting – DaniCee Jan 04 '19 at 05:37
  • Ohh, okay, I was thinking that you need a short cut instead of writing multiple times – akrun Jan 04 '19 at 05:38
  • 1
    There is a `mixedorder` function in `gtools` package. Do you need `mydf[gtools::mixedorder(mydf$A), ]` ? – Ronak Shah Jan 04 '19 at 05:39
  • I was actually looking at `mixedsort`, but I always get the error `argument lengths differ`, I don't seem to be able to apply it to multiple columns – DaniCee Jan 04 '19 at 05:41
  • There is a `multi.mixedorder` function defined in the marked link. You can use that for multiple columns. – Ronak Shah Jan 04 '19 at 05:47
  • That does not work, I tried `mydf3 <- mydf[multi.mixedorder(mydf[,1], mydf[,2], mydf[,3]),]` with the `multi.mixedorder` function in the link, and I still get the order `N-1`, `N-13`, `N-19`, `N-3`, `N-4`, instead of `N-1`, `N-3`, `N-4`, `N-13`, `N-19` for `mydf$A` – DaniCee Jan 04 '19 at 06:51
  • Any help please? The suggested solution in the "duplicated" question does not work for this case!! – DaniCee Jan 08 '19 at 09:46
  • Anyone with the same problem, see https://stackoverflow.com/questions/54089471/r-mixedsort-on-multiple-vectors-columns/54089814#54089814 – DaniCee Jan 08 '19 at 10:28

0 Answers0