Saturday, June 25, 2011

R - sorting vectors: sort() vs. order()

sort() and order()

  • both operates on vectors and has the same aim, but the results are very different
First we create a vector we can sort:
x <- sample(LETTERS[1:10], 100, replace=T) # create a vector to sort by sampling from the first 10 Letters of the alphabet 100 times
x
  [1] "H" "D" "I" "E" "F" "B" "E" "G" "D" "A" "H" "I" "E" "A" "E" "A" "I" "J"
 [19] "I" "A" "B" "F" "A" "I" "F" "B" "A" "H" "J" "A" "E" "A" "C" "A" "A" "C"
 [37] "F" "C" "D" "G" "I" "I" "B" "J" "J" "D" "I" "J" "G" "J" "A" "B" "B" "C"
 [55] "A" "B" "D" "E" "D" "D" "E" "J" "A" "J" "G" "D" "A" "B" "D" "I" "F" "H"
 [73] "D" "J" "D" "E" "E" "A" "A" "J" "B" "E" "C" "I" "C" "F" "F" "E" "E" "J"
 [91] "H" "H" "F" "I" "A" "I" "H" "I" "I" "I"
  • the result of sort() is a vector consisting of elements of the original (unsorted) vector
sort(x)
  [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B"
 [19] "B" "B" "B" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "C" "D" "D" "D" "D"
 [37] "D" "D" "D" "D" "D" "D" "D" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E"
 [55] "E" "F" "F" "F" "F" "F" "F" "F" "F" "G" "G" "G" "G" "H" "H" "H" "H" "H"
 [73] "H" "H" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "J"
 [91] "J" "J" "J" "J" "J" "J" "J" "J" "J" "J"
  • if we do the same with order the result is completely different - we get a vector with the ordered indices of the original (unsorted) vector
order(x)
  [1]  10  14  16  20  23  27  30  32  34  35  51  55  63  67  78  79  95   6
 [19]  21  26  43  52  53  56  68  81  33  36  38  54  83  85   2   9  39  46
 [37]  57  59  60  66  69  73  75   4   7  13  15  31  58  61  76  77  82  88
 [55]  89   5  22  25  37  71  86  87  93   8  40  49  65   1  11  28  72  91
 [73]  92  97   3  12  17  19  24  41  42  47  70  84  94  96  98  99 100  18
 [91]  29  44  45  48  50  62  64  74  80  90
  • if you want to get the ordered vector (like with sort()) you have to index the original vector with the vector of the indices
x[order(x)]
  [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B"
 [19] "B" "B" "B" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "C" "D" "D" "D" "D"
 [37] "D" "D" "D" "D" "D" "D" "D" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E"
 [55] "E" "F" "F" "F" "F" "F" "F" "F" "F" "G" "G" "G" "G" "H" "H" "H" "H" "H"
 [73] "H" "H" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "J"
 [91] "J" "J" "J" "J" "J" "J" "J" "J" "J" "J"

No comments :

Post a Comment