Convenience Methods to Sort Data Frames and Data Sets

Share:

Description

The method below returns a sorted version of the data set given as first argument.

Usage

1
2
## S3 method for class 'data.set'
sort(x,decreasing=FALSE,by=NULL,na.last=NA,...)

Arguments

x

a data frame or data set.

decreasing

a logical value, should sorting be in increasing or decreasing order?

by

a character name of variable names, by which to sort; a formula giving the variables, by which to sort; NULL, in which case, the data frame / data set is sorted by all of its variables.

na.last

for controlling the treatment of 'NA's. If 'TRUE', missing values in the data are put last; if 'FALSE', they are put first; if 'NA', they are removed

...

other arguments, currently ignored.

Value

A sorted copy of x.

Examples

1
2
3
4
5
6
7
8
DS <- data.set(
        a = sample(1:2,size=20,replace=TRUE),
        b = sample(1:4,size=20,replace=TRUE))
sort(DS)
sort(DS,by=~a+b)
sort(DS,by=~b+a)
sort(DS,by=c("b","a"))
sort(DS,by=c("a","b"))