[R] question about order
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Tue Jan 29 02:36:20 CET 2008
On Mon, 28 Jan 2008, 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
Seems ok to me. Maybe you are looking for rank()?
Order gives the ordering permutation, rank the inverse, i.e.:
R> z2 <- sort(z)
R> identical(z2, z[order(z)])
[1] TRUE
R> identical(z, z2[rank(z)])
[1] TRUE
hth,
Z
> 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