
Last chance! 50% off unlimited learning
Sale ends in
Efficiently reorder columns in a data frame. To do this fully by reference see also data.table::setcolorder
.
colorder(.X, ..., pos = "front")colorderv(X, neworder = radixorder(names(X)),
pos = "front", regex = FALSE, ...)
.X/X
with columns reordered (no deep copies).
a data frame or list.
for colorder
: Column names of .X
in the new order (can also use sequences i.e. col1:coln, newname = colk, ...
). For colorderv
: Further arguments to grep
if regex = TRUE
.
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
).
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 (the default). | ||
2 | "end" | move specified columns to the end. | ||
3 | "exchange" | just exchange the positions of selected columns, other columns remain in the same position. | ||
4 | "after" | place all further selected columns behind the first selected column. |
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.
roworder
, Data Frame Manipulation, Collapse Overview
head(colorder(mtcars, vs, cyl:hp, am))
head(colorder(mtcars, vs, cyl:hp, am, pos = "end"))
head(colorder(mtcars, vs, cyl:hp, am, pos = "after"))
head(colorder(mtcars, vs, cyl, pos = "exchange"))
head(colorder(mtcars, vs, cyl:hp, new = am)) # renaming
## 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:4, 9), pos = "after"))
head(colorderv(mtcars, c(8, 2), pos = "exchange"))
Run the code above in your browser using DataLab