Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

colorder

Fast Reordering of Data Frame Columns


Description

Efficiently reorder columns in a data frame (no copies). To do this by reference see also data.table::setcolorder.

Usage

colorder(X, ..., pos = c("front", "end", "exchange"))

colorderv(X, neworder = radixorder(names(X)), pos = c("front","end","exchange"),
          regex = FALSE, ...)

Arguments

X

a data frame or list.

...

for colorder: Column names of X in the new order (can also use sequences i.e. col1:coln, colk, ...). For colorderv: Further arguments to grep if regex = TRUE.

neworder

a vector of column names, positive indices, a suitable logical vector, a function such as is.numeric, or a vector of regular expressions matching column names (if regex = TRUE).

pos

integer or character. Different options regarding column arrangement if ...length() < ncol(X) (or length(neworder) < ncol(X)).

Int. String Description
1 "front" move specified columns to the front of X (the default).
2 "end" move specified columns to the end of X.
3 "exchange" just exchange the positions of selected columns in X, other columns remain in the same position.
regex

logical. TRUE will do regular expression search on the column names of X using a (vector of) regular expression(s) passed to neworder. Matching is done using grep. Note that multiple regular expressions will be matched in the order they are passed, and funique will be applied to the resulting set of indices.

Value

X with columns re-ordered (no deep copy).

See Also

Examples

head(colorder(mtcars, vs, cyl:hp, am))
head(colorder(mtcars, vs, cyl:hp, am, pos = "end"))
head(colorder(mtcars, vs, cyl, pos = "exchange"))

## Same in standard evaluation
head(colorderv(mtcars, c(8, 2:4, 9)))
head(colorderv(mtcars, c(8, 2:4, 9), pos = "end"))
head(colorderv(mtcars, c(8, 2), pos = "exchange"))

collapse

Advanced and Fast Data Transformation

v1.5.3
GPL (>= 2) | file LICENSE
Authors
Sebastian Krantz [aut, cre], Matt Dowle [ctb], Arun Srinivasan [ctb], Laurent Berge [ctb], Dirk Eddelbuettel [ctb], Josh Pasek [ctb], Kevin Tappe [ctb], R Core Team and contributors worldwide [ctb], Martyn Plummer [cph], 1999-2016 The R Core Team [cph]
Initial release
2021-03-05

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.