Blame | Last modification | View Log | Download | RSS feed
% File src/library/base/man/sort_by.Rd% Part of the R package, https://www.R-project.org% Copyright 2024 R Core Team% Distributed under GPL 2 or later\name{sort_by}\alias{sort_by}\alias{sort_by.default}\alias{sort_by.data.frame}\title{Sorting Vectors or Data Frames by Other Vectors}\description{Generic function to sort an object in the order determined by one ormore other objects, typically vectors. A method is defined for dataframes to sort its rows (typically by one or more columns), and thedefault method handles vector-like objects.}\usage{sort_by(x, y, \dots)\method{sort_by}{default}(x, y, \dots)\method{sort_by}{data.frame}(x, y, \dots)}\arguments{\item{x}{An object to be sorted, typically a vector or data frame. }\item{y}{Variables to sort by.For the default method, this can be a vector, or more generally anyobject that has a \code{\link{xtfrm}} method.For the \code{data.frame} method, typically a formula specifying thevariables to sort by. The formula can take the forms \code{ ~ g} or\code{~ list(g)} to sort by the variable \code{g}, or more generallythe forms \code{ ~ g1 + \dots + gk} or \code{~ list(g1, \dots, gk)}to sort by the variables \code{g1}, \dots, \code{gk}, using thelater ones to resolve ties in the preceding ones. These variablesare evaluated in the data frame \code{x} using the usualnon-standard evaluation rules. If not a formula, \code{y = g} isequivalent to \code{y = ~ g} and \code{y = list(g1, \dots, gk)} isequivalent to \code{y = ~ list(g1, \dots, gk)}. However,non-standard evaluation in \code{x} is not done in this case.}\item{\dots}{Additional arguments, typically passed on to\code{\link{order}}. These may include additional variables to sortby, as well as named arguments recognized by \code{order}.}}\value{A sorted version of \code{x}. If \code{x} is a data frame, this meansthat the rows of \code{x} have been reordered to sort the variablesspecified in \code{y}.}\seealso{\code{\link{sort}}, \code{\link{order}}.}\examples{mtcars$ammtcars$mpgwith(mtcars, sort_by(mpg, am)) # group mpg by am## data.frame methodsort_by(mtcars, runif(nrow(mtcars))) # random row permutationsort_by(mtcars, list(mtcars$am, mtcars$mpg))# formula interfacesort_by(mtcars, ~ am + mpg) |> subset(select = c(am, mpg))sort_by.data.frame(mtcars, ~ list(am, -mpg)) |> subset(select = c(am, mpg))}\keyword{univar}\keyword{manip}