[R] sort question in a dataset?

Richard M. Heiberger rmh at temple.edu
Sun Oct 15 07:31:53 CEST 2006


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)]



More information about the R-help mailing list