View source: R/roworder_colorder_rename.R
roworder | R Documentation |
A fast substitute for dplyr::arrange
, based on radixorder(v)
and inspired by data.table::setorder(v)
. It returns a sorted copy of the data frame, unless the data is already sorted in which case no copy is made. In addition, rows can be manually re-ordered. roworderv
is a programmers version that takes vectors/variables as input.
Use data.table::setorder(v)
to sort a data frame without creating a copy.
roworder(X, ..., na.last = TRUE, verbose = .op[["verbose"]])
roworderv(X, cols = NULL, neworder = NULL, decreasing = FALSE,
na.last = TRUE, pos = "front", verbose = .op[["verbose"]])
X |
a data frame or list of equal-length columns. | ||||||||||||||||||||||||||
... |
comma-separated columns of | ||||||||||||||||||||||||||
cols |
select columns to sort by using a function, column names, indices or a logical vector. The default | ||||||||||||||||||||||||||
na.last |
logical. If | ||||||||||||||||||||||||||
decreasing |
logical. Should the sort order be increasing or decreasing? Can also be a vector of length equal to the number of arguments in | ||||||||||||||||||||||||||
neworder |
an ordering vector, can be | ||||||||||||||||||||||||||
pos |
integer or character. Different arrangement options if
| ||||||||||||||||||||||||||
verbose |
logical. |
A copy of X
with rows reordered. If X
is already sorted, X
is simply returned.
If you don't require a copy of the data, use data.table::setorder
(you can also use it in a piped call as it invisibly returns the data).
roworder(v)
has internal facilities to deal with grouped and indexed data. This is however inefficient (since in most cases data could be reordered before grouping/indexing), and therefore issues a message if verbose > 0L
.
colorder
, Data Frame Manipulation, Fast Grouping and Ordering, Collapse Overview
head(roworder(airquality, Month, -Ozone))
head(roworder(airquality, Month, -Ozone, na.last = NA)) # Removes the missing values in Ozone
## Same in standard evaluation
head(roworderv(airquality, c("Month", "Ozone"), decreasing = c(FALSE, TRUE)))
head(roworderv(airquality, c("Month", "Ozone"), decreasing = c(FALSE, TRUE), na.last = NA))
## Custom reordering
head(roworderv(mtcars, neworder = 3:4)) # Bring rows 3 and 4 to the front
head(roworderv(mtcars, neworder = 3:4, pos = "end")) # Bring them to the end
head(roworderv(mtcars, neworder = mtcars$vs == 1)) # Bring rows with vs == 1 to the top
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.