[R] I don't understand the 'order' function

arun smartpink111 at yahoo.com
Tue Apr 16 19:59:06 CEST 2013


Hi,
vec1<- c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)
vec1[order(vec1)]
 #[1]  210  210  505  920 1045 1210 1335 1545 2085 2255 2465
order(vec1)
 #[1]  8  9 10  7 11  6  5  4  3  2  1
sort(vec1,index.return=TRUE)
#$x
 #[1]  210  210  505  920 1045 1210 1335 1545 2085 2255 2465

#$ix
# [1]  8  9 10  7 11  6  5  4  3  2  1
A.K.



----- Original Message -----
From: Julio Sergio <juliosergio at gmail.com>
To: r-help at stat.math.ethz.ch
Cc: 
Sent: Tuesday, April 16, 2013 1:51 PM
Subject: [R] I don't understand the 'order' function

I thought I've understood the 'order' function, using simple examples like:

   order(c(5,4,-2))
   [1] 3 2 1

However, I arrived to the following example:

   order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) 
   [1]  8  9 10  7 11  6  5  4  3  2  1

and I was completely perplexed!
Shouldn't the output vector be  11 10 9 8 7 6 4 1 2 3 5 ?
Do I have a damaged version of R?

I became still more astonished when I used the sort function and got the 
right answer: 

   sort(c(2465, 2255, 2085, 1545, 1335, 1210,  920,  210,  210,  505, 1045))
   [1]  210  210  505  920 1045 1210 1335 1545 2085 2255 2465
since 'sort' documentation claims to be using 'order' to establish the right 
order.

Please help me to understand all this!

  Thanks,

  -Sergio.

______________________________________________
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