Daniel Brewer
2009-Jun-24 14:43 UTC
[R] order by decerasing 1st variable and increasing 2nd variable
Hello, I have a data.frame which I would like to sort with the primary key decreasing while the secondry key is increasing e.g. x <- data.frame(One=c(1,1,1,2,2,3,4,5),Two=c(2,3,1,2,3,3,3,3)) I would like to order it so it looks like this: One Two 8 5 3 7 4 3 6 3 3 4 2 2 5 2 3 3 1 1 1 1 2 2 1 3 i.e. primarily decreasing in the 1st column but if there is a tie increasing in the second column. Is this possible? I can not find anything in order that seems to support this. I would have thought, x[order(x$One,x$Two,decreasing=c(T,F)),] would do it but it doesn't. Thanks Dan -- ************************************************************** Daniel Brewer, Ph.D. Institute of Cancer Research Molecular Carcinogenesis Email: daniel.brewer at icr.ac.uk ************************************************************** The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. This e-mail message is confidential and for use by the a...{{dropped:2}}
Ronggui Huang
2009-Jun-24 14:54 UTC
[R] order by decerasing 1st variable and increasing 2nd variable
How about this:> x[order(x$One,-x$Two,decreasing=T),]One Two 8 5 3 7 4 3 6 3 3 4 2 2 5 2 3 3 1 1 1 1 2 2 1 3 2009/6/24 Daniel Brewer <daniel.brewer at icr.ac.uk>:> Hello, > > I have a data.frame which I would like to sort with the primary key > decreasing while the secondry key is increasing e.g. > > x <- data.frame(One=c(1,1,1,2,2,3,4,5),Two=c(2,3,1,2,3,3,3,3)) > > I would like to order it so it looks like this: > > ?One Two > 8 ? 5 ? 3 > 7 ? 4 ? 3 > 6 ? 3 ? 3 > 4 ? 2 ? 2 > 5 ? 2 ? 3 > 3 ? 1 ? 1 > 1 ? 1 ? 2 > 2 ? 1 ? 3 > > i.e. primarily decreasing in the 1st column but if there is a tie > increasing in the second column. > > Is this possible? ?I can not find anything in order that seems to > support this. ?I would have thought, > > x[order(x$One,x$Two,decreasing=c(T,F)),] > > would do it but it doesn't. > > Thanks > > Dan > > -- > ************************************************************** > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > Email: daniel.brewer at icr.ac.uk > ************************************************************** > > The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. > > This e-mail message is confidential and for use by the a...{{dropped:2}} > > ______________________________________________ > 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. >-- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html