[R] multicolumn sort on dataframe?
Anne York
york at zipcon.net
Sat Mar 27 18:39:43 CET 2004
On Fri 26 Mar 2004, Jeff D. Hamann wrote:
>I couldn't find any reference to this in the FAQ, but is it
>possible to sort a dataframe by multiple columns?
>I've created some code, similar to the following:
>nspr.code <- sp.results$sp.code[order( sp.results$sp.code )]
>nspr.tpa <- sp.results$tpa[order( sp.results$sp.code )]
>nspr.code <- as.character( levels( nspr.code ) )[nspr.code]
>nspr.tpa <- as.numeric( levels( nspr.tpa ) )[nspr.tpa]
>hope <- as.data.frame( cbind( nspr.code, as.numeric(nspr.tpa) ) )
A simple way to sort multiple columns is to paste them
together and sort the resulting character vector. THat way
you only have to do one sort. This is a very old method
taught to me in the first computer course I ever took (date
censored); the instructor attributed the method to Von
Neumann but I have no reference.
You have to be careful choosing the sep character in paste.
Here is an example
> set.seed(78)
> foo = data.frame(x= sample(letters[1:3],5,replace=TRUE),
y= sample(1:5,5,replace=TRUE))
> foo
x y
1 c 3
2 c 2
3 b 2
4 c 1
5 c 3
Sorting on y and then by x:
> my.order=order(foo.paste=paste(foo[,2],foo[,1],sep="/"))
> my.order
[1] 4 3 2 1 5
> my.order=order(paste(foo[,1],foo[,2],sep="/"))
> foo[my.order,]
x y
3 b 2
4 c 1
2 c 2
1 c 3
5 c 3
>
More information about the R-help
mailing list