[R] Reimplement order somehow

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Oct 3 14:45:29 CEST 2007


You're sorting characters in the PHP script instead of numbers! 

sort(as.character(o)) in R will yield the same results as you PHP script
does.

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney



> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org 
> [mailto:r-help-bounces op r-project.org] Namens XpeH
> Verzonden: woensdag 3 oktober 2007 14:38
> Aan: r-help op r-project.org
> Onderwerp: [R] Reimplement order somehow
> 
> 
> Hello,
> 
> I have a script in R language that makes sorting using the 
> order() method of R language. What I need is to reimplement 
> this method in any other language (PHP, Perl, Python, Java maybe).
> 
> First I tried to reimplement it in php, here is some numbers 
> that i need to
> sort:
> 1,2,3,4,5,6,7,8,9,10,300,231,11,12,0,1
> 
> R language:
> n = c(1,2,3,4,5,6,7,8,9,10,300,231,11,12,0,1)
> n
> [1]   1   2   3   4   5   6   7   8   9  10 300 231  11  12   0   1
> order(n)
> [1] 15  1 16  2  3  4  5  6  7  8  9 10 13 14 12 11
> 
> PHP
> <?php
> $string = "1 2 3 4 5 6 7 8 9 10 300 231 11 12 0 1"; $array = 
> explode(" ", $string);
> 
> foreach ($array as $key => $value) {
>     $array_new[$key + 1] = $value;
> }
> $array = $array_new;
> 
> asort($array);
> foreach ($array as $key => $value) {
>     print " $key";
> }
> ?>
> 
> output from php is: 15 1 16 2 3 4 5 6 7 8 9 10 13 14 12 1 
> (same as from the R script), now I use another numbers:
> 
> 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 
> 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 
> 11 11 11 11 11 11 11 11 11 11
> 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 
> 13 13 13 13 13
> 13 13 13 13 13 13 13 13 13 13 13 13 13 13 2 2 2 2 2 2 2 2 2 2 
> 2 2 2 2 2 2 2
> 2 2 255 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 
> 4 4 4 4 4 4 4
> 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 
> 6 6 6 6 6 6 6
> 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 
> 8 8 8 8 8 8 8
> 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
> 
> R language:
> o = c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
> 1, 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
> 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
> 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 
> 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 
> 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 
> 13, 13, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
> 2, 255, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
> 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 
> 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 
> 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 
> 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 
> 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9)
> 
>  order(o)
>   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14 
>  15  16  17 
> 18
>  [19]  19  20  97  98  99 100 101 102 103 104 105 106 107 108 
> 109 110 111
> 112
>  [37] 113 114 115 117 118 119 120 121 122 123 124 125 126 127 
> 128 129 130
> 131
>  [55] 132 133 134 135 136 137 138 139 140 141 142 143 144 145 
> 146 147 148
> 149
>  [73] 150 151 152 153 154 155 156 157 158 159 160 161 162 163 
> 164 165 166
> 167
>  [91] 168 169 170 171 172 173 174 175 176 177 178 179 180 181 
> 182 183 184
> 185
> [109] 186 187 188 189 190 191 192 193 194 195 196 197 198 199 
> 200 201 202
> 203
> [127] 204 205 206 207 208 209 210 211 212 213 214 215 216 217 
> 218 219 220
> 221
> [145] 222 223 224 225 226 227 228 229 230 231 232 233 234 235 
> 236 237 238
> 239
> [163] 240 241 242 243 244 245 246 247 248 249  21  22  23  24 
>  25  26  27
> 28
> [181]  29  30  31  32  33  34  35  36  37  38  39  40  41  42 
>  43  44  45
> 46
> [199]  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
>  61  62  63
> 64
> [217]  65  66  67  68  69  70  71  72  73  74  75  76  77  78 
>  79  80  81
> 82
> [235]  83  84  85  86  87  88  89  90  91  92  93  94  95  96 116
> 
> --------------------
> 
> Now the php code:
> 
> <?php
> $str = "0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 
> 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 
> 11 11 11 11 11 11 11 11 11 11
> 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 
> 12 12 12 13 13
> 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 2 2 2 2 2 
> 2 2 2 2 2 2 2 2
> 2 2 2 2 2 2 255 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 
> 4 4 4 4 4 4 4
> 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 
> 6 6 6 6 6 6 6
> 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 
> 8 8 8 8 8 8 8
> 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9"; 
> $array = explode(" ", $str);
> 
> foreach ($array as $key => $value) {
>     $array_new[$key + 1] = $value;
> }
> $array = $array_new;
> 
> asort($array);
> foreach ($array as $key => $value) {
>     print " $key";
> }
> ?>
> 
> 
> and output is:
> 
> 1 14 13 12 15 17 19 18 11 16 20 4 3 2 10 5 6 8 9 7 109 107 
> 108 114 106 113
> 112 111 110 102 99 98 115 100 101 105 104 103 97 127 131 130 
> 129 132 133 135
> 134 128 126 120 119 118 121 122 124 123 117 125 142 143 144 
> 141 140 137 138
> 139 145 146 151 152 153 150 149 147 148 136 154 161 162 164 
> 160 159 156 157
> 158 165 166 171 172 173 170 169 167 168 155 163 187 185 184 
> 188 189 192 191 190 183 186 177 176 182 174 178 175 179 180 
> 181 199 201 200 197 194 202 193
> 195 196 198 208 209 203 211 207 210 206 205 204 223 221 222 
> 224 227 229 228
> 226 225 220 213 212 219 214 230 215 217 218 216 237 238 239 
> 236 233 231 232
> 234 235 240 247 248 241 246 249 242 245 243 244 27 29 26 28 
> 23 30 22 24 35
> 37 38 39 36 21 32 33 34 31 25 46 47 48 45 44 41 42 43 49 50 
> 55 56 57 54 53
> 51 52 40 58 71 70 69 72 73 76 75 74 68 67 61 60 77 62 63 66 
> 65 64 59 79 91 90 89 92 93 96 95 94 80 78 83 82 81 88 84 87 86 85 116
> 
> 
> That is definetly not the same as from the r language order() 
> method...
> :confused:
> 
> 
> Does anyone knows how to sort this numbers somehow in some 
> language rather than R?
> Thanks.
> --
> View this message in context: 
> http://www.nabble.com/Reimplement-order-somehow-tf4561576.html
> #a13018078
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help op r-project.org mailing list
> 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.
> 



More information about the R-help mailing list