Argh. I knew it was at least partly obvious. I never have been able to read the order() help page and understand what it is saying.
THanks very much. By the way, to me it is counter-intuitive that the the command is > df1[order(df1[,2],decreasing=TRUE),] For some reason I keep expecting it to be order( , df1[,2],decreasing=TRUE) So clearly I don't understand what is going on but at least I a lot better off. I may be able to get this graph to work. --- On Thu, 5/12/11, Patrick Breheny <patrick.breh...@uky.edu> wrote: > From: Patrick Breheny <patrick.breh...@uky.edu> > Subject: Re: [R] Simple order() data frame question. > To: "John Kane" <jrkrid...@yahoo.ca> > Cc: "R R-help" <r-h...@stat.math.ethz.ch> > Received: Thursday, May 12, 2011, 8:44 AM > On 05/12/2011 08:32 AM, John Kane > wrote: > > Clearly, I don't understand what order() is doing and > as ususl the help for order seems to only confuse me more. > For some reason I just don't follow the examples there. I > must be missing something about the data frame sort there > but what? > > > > I originally wanted to reverse-order my data > frame df1 (see below) by aa (a factor) but since this was > not working I decided to simplify and order by bb to see > what was haqppening!! > > > > I'm obviously doing something stupid but what? > > > > (df1<- data.frame(aa=letters[1:10], > > bb=rnorm(10))) > > # Order in acending order by bb > > (df1[order(df1[,2]),] ) # seems to work fine > > > > # Order in decending order by bb. > > (df1[order(df1[,-2]),]) # does not seem to work > > > > There is a 'decreasing' option described in the help file > for 'order' > which does what you want: > > df1<- data.frame(aa=letters[1:10],bb=rnorm(10)) > df1[order(df1[,2],decreasing=TRUE),] > > aa bb > 6 f 3.16449690 > 7 g 2.44362935 > 8 h 0.80990322 > 1 a 0.06365513 > 5 e -0.33932586 > 9 i -0.52119533 > 2 b -0.65623164 > 4 d -0.86918700 > 3 c -1.86750927 > 10 j -2.21178676 > > df1[order(df1[,1],decreasing=TRUE),] > > aa bb > 10 j -2.21178676 > 9 i -0.52119533 > 8 h 0.80990322 > 7 g 2.44362935 > 6 f 3.16449690 > 5 e -0.33932586 > 4 d -0.86918700 > 3 c -1.86750927 > 2 b -0.65623164 > 1 a 0.06365513 > > The expression 'df1[,-2]' removes the second column from > df1; clearly > not what you want here. > > -- > Patrick Breheny > Assistant Professor > Department of Biostatistics > Department of Statistics > University of Kentucky > ______________________________________________ R-help@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.