Dear friends, I want to sort a dataset according to one or two variables in the dataset, i thought sort could do it , but failed. e.g. x <- c(2, 9, 18, 3, 2) y<-c(2,5.6,5,9,8) z<-c(21,5,5,19,7) a<-cbind(x,y,z) a x y z [1,] 2 2.0 21 [2,] 9 5.6 5 [3,] 18 5.0 5 [4,] 3 9.0 19 [5,] 2 8.0 7 I want to sort dataset a according to ascending x and descending y, How to accomplish it? BTW, sort(a$x) can't works and it shows"null", why is it? Any suggestions are welcome! -- With Kind Regards, oooO::::::::: (..)::::::::: :\.(:::Oooo:: ::\_)::(..):: :::::::)./::: ::::::(_/:::: ::::::::::::: [***********************************************************************] Zhi Jie,Zhang ,PHD Tel:86-21-54237149 epistat@gmail.com Dept. of Epidemiology,school of public health,Fudan University Address:No. 138 Yi Xue Yuan Road,Shanghai,China Postcode:200032 [***********************************************************************] oooO::::::::: (..)::::::::: :\.(:::Oooo:: ::\_)::(..):: :::::::)./::: ::::::(_/:::: ::::::::::::: [[alternative HTML version deleted]]
> ?order > x <- c(2, 9, 18, 3, 2) > y <- c(2,5.6,5,9,8) > z <- c(21,5,5,19,7) > a <- cbind(x, y, z) > ax y z [1,] 2 2.0 21 [2,] 9 5.6 5 [3,] 18 5.0 5 [4,] 3 9.0 19 [5,] 2 8.0 7> aa <- a[order(a[,"y"], decreasing=TRUE),] > aaa <- aa[order(aa[,"x"], decreasing=FALSE),] > aaax y z [1,] 2 8.0 7 [2,] 2 2.0 21 [3,] 3 9.0 19 [4,] 9 5.6 5 [5,] 18 5.0 5>a$y doesn't work because $ subscripting requires a data.frame. cbind creates an ordinary matrix. This works with a data.frame.> a <- data.frame(x, y, z) > aa <- a[order(a$y, decreasing=TRUE),] > aaa <- aa[order(aa$x, decreasing=FALSE),]Please use spaces for legibility on both sides of the assignment arrow and after a comma. If you want all columns ascending (or descending), then you could do it in one step> aaaa <- a[order(a$x, a$y), ]See also the example in ?order ## Suppose we wanted descending order on y. A simple solution is rbind(x,y,z)[, order(x, -y, z)]
Your desired answer just interchanges the sequence of the steps x <- c(2, 9, 18, 3, 2) y <- c(2,9,8,9,8) z <- c(21,5,5,19,7) a <- cbind(x, y, z) #dataset bb <- a[order(a[,"x"], decreasing=FALSE),] bbb <- bb[order(bb[,"y"], decreasing=TRUE),] bbb>From ?sortSort (or order) a vector or factor (partially) into ascending (or descending) order. For ordering along more than one variable, e.g., for sorting data frames, see order.>From ?orderorder returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. tmp <- c(10,15,12,7) sort(tmp) order(tmp) tmp[order(tmp)]
Another way to do this is: o <- order(a[,"y"],-a[,"x"], decreasing=TRUE) a[o,] -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Richard M. Heiberger Sent: Sunday, October 15, 2006 1:32 AM To: zhijie zhang Cc: R-help at stat.math.ethz.ch Subject: Re: [R] sort question in a dataset? Your desired answer just interchanges the sequence of the steps x <- c(2, 9, 18, 3, 2) y <- c(2,9,8,9,8) z <- c(21,5,5,19,7) a <- cbind(x, y, z) #dataset bb <- a[order(a[,"x"], decreasing=FALSE),] bbb <- bb[order(bb[,"y"], decreasing=TRUE),] bbb>From ?sortSort (or order) a vector or factor (partially) into ascending (or descending) order. For ordering along more than one variable, e.g., for sorting data frames, see order.>From ?orderorder returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. tmp <- c(10,15,12,7) sort(tmp) order(tmp) tmp[order(tmp)] ______________________________________________ R-help at stat.math.ethz.ch 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.