[R] question about order
Benilton Carvalho
bcarvalh at jhsph.edu
Tue Jan 29 02:28:37 CET 2008
that seems right....
order() gives you the indexes "idx" such that x[idx] == sort(x)
> set.seed(123)
> x <- rnorm(10)
> idx <- order(x)
> identical(x[idx], sort(x))
[1] TRUE
best
b
On Jan 28, 2008, at 8:19 PM, Waverley wrote:
> I have a data vector as following:
>> z
> [1] 183.1370 201.9610 113.7250 140.7840 156.2750 42.1569 42.1569
> 42.1569
> [9] 240.1960 308.4310 42.1569 42.1569 42.1569 42.1569 42.1569
> 42.1569
> [17] 42.1569 42.1569 42.1569 42.1569 279.8040 42.1569 42.1569
>
> when I sort, it gave me the right order
>
>> sort(z)
> [1] 42.1569 42.1569 42.1569 42.1569 42.1569 42.1569 42.1569
> 42.1569
> [9] 42.1569 42.1569 42.1569 42.1569 42.1569 42.1569 42.1569
> 113.7250
> [17] 140.7840 156.2750 183.1370 201.9610 240.1960 279.8040 308.4310
>
> BUT when I use the order, the returned index is strange and not right.
> You can check the first 4 values.
>> order (z)
> [1] 6 7 8 11 12 13 14 15 16 17 18 19 20 22 23 3 4 5 1 2 9
> 21 10
>
> I am not sure why R does not order it correctly when handling a vector
> with repetitive values.
>
> I use just the first 4 values of z, then it ordered correctly.
>> order (z[1:4])
> [1] 3 4 1 2
>
> Can someone help? What is the problem here? Is this a R bug? How to
> order when handling a vector with repetitive values?
>
> --
> Waverley @ Palo Alto
>
> ______________________________________________
> R-help at 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