Sort data.frame Objects

Share:

Description

Sort a data.frame by any column(s).

Usage

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

Arguments

x

A data.frame.

decreasing

Logical, sort in decreasing order. See also sort.

by

Index (integer) or names of columns in x to sort by in that order. If both by and bye are missing, all columns are used to sort in their order.

bye

Unquoted column name or list() or .() with unquoted column names to sort x by. Not evaluated if by is supplied.

na.last

TRUE to put missing values last, FALSE to put first or NA to remove.

...

Ignored for the data.frame method.

Author(s)

Sven E. Templer

Examples

1
2
3
4
5
6
7
8
9
#

d <- data.frame(a=c(1,1,1,2,NA),b=c(2,1,3,1,1),c=5:1)
d
sort(d) # sort by every column (a, then b, then c)
sort(d, TRUE, by="c") # decreasing by column 'c'
sort(d, bye=.(a,c)) # increasing by columns 'a' and then 'c'

#