Reorder columns in an R dataframe
How to reorder columns in an R data frame with dplyr's relocate() and select(), base R indexing and data.table, plus what reordering changes and when to keep the original order.
Short answer
If you work in the tidyverse, relocate() from dplyr is usually the best way to change column order. It moves the columns you name and keeps every other column, so you are unlikely to drop data by accident. select() combined with everything() also works and is useful when you want to reorder, rename and subset in one step. The key difference is that select() drops any column you do not mention. Base R indexing and data.table's setcolorder() are good alternatives outside dplyr. Keep the existing order when downstream code, file schemas or other people's scripts refer to columns by position rather than by name.
What reordering actually changes
Reordering changes only the position of columns in the data frame. Cell values, column types, column names (unless you rename on purpose), row order and grouping all stay the same. dplyr functions return a new data frame and leave the original untouched, so the new order persists only if you assign the result. data.table's setcolorder() is different: it modifies the table in place.
In text-transformation tools, 'reverse' and 'mirror' usually mean changing the characters inside a string. Reordering columns does nothing like that. No text in any cell is altered. Reversing the order of columns is a separate operation that changes only column sequence, as shown below.
Example data
library(dplyr)
df <- tibble(
score = c(90, 85, 77),
name = c("Ana", "Ben", "Cy"),
id = 1:3,
group = c("a", "b", "a")
)
# columns: score, name, id, group
Moving columns with relocate()
relocate(df, id)
# id, score, name, group
relocate(df, id, name)
# id, name, score, group
relocate(df, group, .before = score)
# group, score, name, id
relocate(df, score, .after = last_col())
# name, id, group, score
relocate(df, where(is.character))
# name, group, score, id
# with the native pipe
df |> relocate(id)
By default, relocate() moves the selected columns to the front. Use .before or .after to choose a different position, but not both in the same call. relocate() accepts tidyselect helpers such as where(), starts_with() and last_col(), so you can move groups of columns by type or naming pattern. It was added in dplyr 1.0.0, so older installations won't have it.
Reordering with select() and everything()
select(df, id, name, everything())
# id, name, score, group
select(df, id, name)
# id, name (score and group are dropped)
select(df, id, student = name, everything())
# id, student, score, group
everything() picks up every column you haven't already selected, so no column is duplicated. Without it, select() keeps only the columns you list. That is useful for subsetting but risky if you only meant to reorder. select() can also rename columns as it places them. On grouped data, select() keeps the grouping variables even if you leave them out, and prints a message saying it added them.
Using a stored order, alphabetical order or reversed order
new_order <- c("id", "name", "score", "group")
select(df, all_of(new_order))
# id, name, score, group (errors if any name is missing)
select(df, any_of(c("id", "name", "email")), everything())
# id, name, score, group (missing "email" is skipped silently)
select(df, all_of(sort(names(df))))
# group, id, name, score
select(df, all_of(rev(names(df))))
# group, id, name, score
When the order comes from a character vector, such as a config file or a schema, wrap it in all_of() or any_of(). all_of() stops with an error if a listed column doesn't exist, which helps catch schema drift. any_of() skips missing names, which suits optional columns but can hide typos. In this small example, alphabetical and reversed order happen to produce the same result. They will differ on other data.
Base R and data.table
# base R
df[c("id", "name", "score", "group")]
df[c("id", setdiff(names(df), "id"))] # move id to the front
df[, "id", drop = FALSE] # keep a data frame when selecting one column
# data.table
library(data.table)
dt <- as.data.table(df)
setcolorder(dt, c("id", "name"))
# dt is now id, name, score, group; changed in place, no assignment needed
Base R needs no packages, but you must handle the remaining columns yourself, for example with setdiff(). With a plain data.frame, single-column selection using df[, "x"] returns a vector unless you add drop = FALSE. Tibbles don't simplify this way. setcolorder() moves the listed columns to the front and keeps the rest in their current order. Because it changes the table in place, every reference to that data.table sees the new order.
Choosing an approach
| Method | Keeps unlisted columns | Can rename | Modifies in place | Best for |
| relocate() | Yes | No | No | Moving a few columns within a tidyverse pipeline |
| select() + everything() | Yes, with everything() | Yes | No | Reordering, renaming and subsetting together |
| select(all_of(vector)) | No | No | No | Enforcing a fixed, known schema |
| Base R [ ] | No, unless added with setdiff() | No | No | Scripts without package dependencies |
| setcolorder() | Yes | No | Yes | data.table workflows |
Will the new order hold up after export and sharing?
Writers such as write.csv() and readr's write_csv() output columns in the data frame's current order, so anyone opening the file sees the new layout. Operations that match by name are unaffected by column order: joins, bind_rows(), model formulas and most dplyr verbs. Operations that depend on position are affected.
- Code that uses positions such as df[[2]] or df[, 3] will silently pick a different column.
- cbind() and similar positional combinations will line up columns differently.
- Importers or spreadsheets that expect a fixed column layout may misread a file with a new order.
- Colleagues' scripts written against the old order may break without raising an error.
Column order also matters for readability. Placing identifiers and key fields first makes printed output and exported tables easier to scan. Screen readers generally read HTML table cells in column order, so the same principle likely helps assistive-technology users once data is rendered in a report. That point has not been tested here against any specific screen reader or reporting tool. Check your own output if accessibility matters. When a downstream consumer depends on a specific layout, keep that layout or document the change, and refer to columns by name in your own code.
Evidence gaps
No benchmarks, version-specific test runs or measurements were available for this article. The outputs in the code comments are derived from documented function behavior; they were not produced by running the code here. Performance differences between these methods on large data are not covered. If speed matters, measure it on your own data and package versions.