Learn R Programming

expss (version 0.5.5)

sort_asc: Sort data.frames/matrices/vectors

Description

sort_asc sorts in ascending order and sort_desc sorts in descending order. There is no non-standard evaluation in these functions by design so use quotes for names of your variables or use qc. %sort_asc%/%sort_desc% are infix versions of these functions. .sort_asc/.sort_desc are versions for working with default_dataset.

Usage

sort_asc(data, ..., na.last = FALSE)
data %sort_asc% variables
.sort_asc(..., na.last = FALSE)
sort_asc(data) <- value
sort_desc(data, ..., na.last = TRUE)
.sort_desc(..., na.last = TRUE)
data %sort_desc% variables
sort_desc(data) <- value

Arguments

data
data.frame/matrix/vector
...
character/numeric. Column names/numbers for data.frame/matrix by which object will be sorted. Ignored for vectors.
na.last
for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed.
variables
character/numeric. Column names/numbers for data.frame/matrix by which object will be sorted for infix functions. Ignored for vectors.
value
character/numeric. Column names/numbers for data.frame/matrix by which object will be sorted. The same as ... but for assignment versions of functions.

Value

sorted data

Examples

Run this code
data(mtcars)
sort_asc(mtcars, "mpg")
sort_asc(mtcars, "cyl", "mpg") # by two column

# same results with column nums
sort_asc(mtcars, 1)
sort_asc(mtcars, 2:1) # by two column
sort_asc(mtcars, 2, 1) # by two column

# 'qc'  usage
sort_asc(mtcars, qc(cyl, mpg)) 

# infix version
mtcars %sort_asc% "mpg"
mtcars %sort_asc% c("cyl", "mpg")
mtcars %sort_asc% qc(cyl, mpg) 

Run the code above in your browser using DataLab