Dear all, I have recently experienced something with the function order I cannot explain: The help(order) (which I admit having overlooked before) rises even more my confusion... The following lines made me think order() was returning the 'order' each value in a vector would take when sorted.> a <- c(4.1, 3.2, 6.1) > order(a)[1] 2 1 3 Doing> plot(a, order(a)/length(a))gave what I expected (an empirical distribution function). Hower the following made me think I have been misusing order():> a <- c(4.1, 3.2, 6.1, 3.1) > order(a)[1] 4 2 1 3> > a <- rnorm(50) > plot(a, order(a)/50)My question is, what is really order() doing ? (This question is like asking for RTFMs... I have no problem with that, I just could not find much in the pdf about R I had). Thanking in advance an help, Laurent -- Laurent Gautier CBS, Building 208, DTU PhD. Student D-2800 Lyngby,Denmark tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sat, 24 Nov 2001, Laurent Gautier wrote:> Dear all, > > > I have recently experienced something with the function order I cannot > explain: > > > The help(order) (which I admit having overlooked before) rises even more > my confusion... > > The following lines made me think order() was returning the 'order' each > value in a vector would take when sorted. > > > a <- c(4.1, 3.2, 6.1) > > order(a) > [1] 2 1 3 > > Doing > > plot(a, order(a)/length(a)) > gave what I expected (an empirical distribution function). > > > > Hower the following made me think I have been misusing order(): > > > a <- c(4.1, 3.2, 6.1, 3.1) > > order(a) > [1] 4 2 1 3 > > > > a <- rnorm(50) > > plot(a, order(a)/50) > > My question is, what is really order() doing ?In a sense it's doing the opposite of what you thought. The definition of order() is basically that a[order(a)] is in increasing order. This works with your example, where the correct order is the fourth, second, first, then third element. You may have been looking for rank(), which returns the rank of the elements R> a <- c(4.1, 3.2, 6.1, 3.1) R> order(a) [1] 4 2 1 3 R> rank(a) [1] 3 2 4 1 so rank() tells you what order the numbers are in, order() tells you how to get them in ascending order. plot(a, rank(a)/length(a)) will give a graph of the CDF. To see why order() is useful, though, try plot(a, rank(a)/length(a),type="S") which gives a mess, because the data are not in increasing order If you did oo<-order(a) plot(a[oo],rank(a[oo])/length(a),type="S") or simply oo<-order(a) plot(a[oo],(1:length(a))/length(a)),type="S") you get a line graph of the CDF -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
According to Thomas Lumley : >In a sense it's doing the opposite of what you thought. >The definition of order() is basically that > a[order(a)] >is in increasing order. This works with your example, where the correct >order is the fourth, second, first, then third element. >You may have been looking for rank(), which returns the rank of the >elements />R> a <- c(4.1, 3.2, 6.1, 3.1) / />R> order(a) / >[1] 4 2 1 3 />R> rank(a) / >[1] 3 2 4 1 >so rank() tells you what order the numbers are in, order() tells you how >to get them in ascending order. Hmmm ... meaning that order behaves like the (gradeup) APL function, right ? (Yes, I'm *that* old ...). That should imply that, barring possible ties, rank(x) == order(order(x)). Right ? So : why distinc implementations ? Are there efficiency considerations I'm missing ? Or am I completely mistaken ? -- Emmanuel Charpentier -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
e.g.: to plot with colors 1 and 2 (easiest) and plotting characters 1 and 16: x <- runif(10) y <- runif(10) z <- factor(sample(1:2,size=10,replace=TRUE)) plot(x,y,col=as.numeric(z),pch=c(1,16)[as.numeric(z)]) On Tue, 27 Nov 2001, Erich Neuwirth wrote:> when i need to plot the same variables > (size and weight e.g.) > for different groups (male and female) > in one scatterplot, > i seem have to use plot() for the first group and > points() for the second group. > > is there a plotting function somewhere which would use a factor variable > automatically to represent the different groups > by different symbols and/or colors. > > i know it is not too difficult > to write such a function for oneself, > the question is if this should be part of a standard toolkit already. > > or am i just plainly overlooking something which is already there. > > > by(data,factor,plot) > > could be a "role model". > and this seems not to work. > > > > > -- > Erich Neuwirth, Computer Supported Didactics Working Group > Visit our SunSITE at http://sunsite.univie.ac.at > Phone: +43-1-4277-38624 Fax: +43-1-4277-9386 > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
i had not tried as.integer or as.numeric i just tried the factor variable itself. now i know better. thanks for all the help Ben Bolker wrote:> > e.g.: to plot with colors 1 and 2 (easiest) and plotting characters 1 and > 16: > > x <- runif(10) > y <- runif(10) > z <- factor(sample(1:2,size=10,replace=TRUE)) > plot(x,y,col=as.numeric(z),pch=c(1,16)[as.numeric(z)]) >-- Erich Neuwirth, Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-38624 Fax: +43-1-4277-9386 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._