These function return a data frame sorted in lexcographic order or a permutation that will rearrange it into lexicographic order: first by the first column, ties broken by the second, remaining ties by the third, etc..

order(..., na.last = TRUE, decreasing = FALSE)

# Default S3 method
order(..., na.last = TRUE, decreasing = FALSE)

# S3 method for class 'data.frame'
order(..., na.last = TRUE, decreasing = FALSE)

# S3 method for class 'matrix'
order(..., na.last = TRUE, decreasing = FALSE)

# S3 method for class 'data.frame'
sort(x, decreasing = FALSE, ...)

Arguments

...

Ignored for sort. For order, first argument is the data frame to be ordered. (This is needed for compatibility with order.)

na.last

See order documentation.

decreasing

Whether to sort in decreasing order.

x

A data.frame to sort.

Value

For sort, a data frame, sorted lexicographically. For order, a permutation I (of a vector 1:nrow(x)) such that x[I,,drop=FALSE] equals x ordered lexicographically.

See also

Examples


data(iris)

head(iris)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa

head(order(iris))
#> [1] 14  9 39 43 42  4

head(sort(iris))
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 14          4.3         3.0          1.1         0.1  setosa
#> 9           4.4         2.9          1.4         0.2  setosa
#> 39          4.4         3.0          1.3         0.2  setosa
#> 43          4.4         3.2          1.3         0.2  setosa
#> 42          4.5         2.3          1.3         0.3  setosa
#> 4           4.6         3.1          1.5         0.2  setosa

stopifnot(identical(sort(iris),iris[order(iris),]))