[R] sort question in a dataset?

Christos Hatzis christos at nuverabio.com
Sun Oct 15 07:41:40 CEST 2006


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 ?sort
Sort (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 ?order
order 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.



More information about the R-help mailing list