Reorder Columns with dplyr: What Changes, What Stays, and When to Use It

A practical review of reordering data frame columns in dplyr with relocate() and select(): what each changes, how the result behaves after export or sharing, and when base R or leaving the order alone is better.

Reordering columns in dplyr is a structural change to a data frame. It changes the position of columns but not the values they hold. This review covers the two main tools, relocate() and select(), what each actually changes, and how the reordered result holds up when you export, share or reuse it. The behavior described here follows dplyr's documented functions and general R principles. No specific test results, versions or benchmarks were supplied for this article, so version-sensitive details are flagged rather than asserted.

Is reordering columns with dplyr the right choice?

Recommendation: use relocate() when you want to move a few columns and keep everything else in place. Use select() when you want to choose and order columns in one step, and you accept that any column you do not name is dropped unless you add everything(). The main benefit is readability. You can put identifiers and key measures first, which makes printed output, exported files and review tables easier to scan.

The main tradeoff is that any downstream code or process that refers to columns by position, rather than by name, will now point at different data. That failure is silent. The code still runs, but it uses the wrong column.

Leaving the original order alone, or using base R instead, is preferable in a few situations:

  • The data feeds a fixed-layout consumer, such as a template, import specification or legacy script that expects columns in a set position.
  • Other code indexes columns by number, for example df[, 3], and cannot be updated at the same time.
  • You are writing a small script or package where adding a dplyr dependency only for column order is not worth it. Base R indexing by a vector of names does the same job.
  • Column order carries no meaning for the task, such as an intermediate object that is joined or summarized and never displayed.

What reordering actually changes

Reordering changes only the sequence of columns in the data frame's structure, which is what names() returns. Row order, cell values, column names, column types and the number of rows stay the same. This is a change to stored structure, not a display setting. Once you assign the result, the new order is part of the object and carries through to printing, export and further code.

A note on wording: in this context, "reverse" or "flip" means reversing the order of columns. It does not mean reversing characters inside text values or mirroring how text looks. None of the functions below touch the contents of character columns.

relocate(): move selected columns, keep the rest

relocate() moves the columns you name and leaves all other columns in their existing relative order. By default the named columns go to the front. The .before and .after arguments place them relative to another column, and last_col() targets the end. relocate() never drops columns.

library(dplyr)

names(mtcars)
# "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

mtcars %>% relocate(hp, wt) %>% names()
# "hp"   "wt"   "mpg"  "cyl"  "disp" "drat" "qsec" "vs"   "am"   "gear" "carb"

mtcars %>% relocate(gear, .after = mpg)
mtcars %>% relocate(mpg, .after = last_col())
mtcars %>% relocate(where(is.character), .before = 1)

The example output above follows from the documented rule: the named columns move to the front in the order given, and the remaining columns keep their original sequence. The last line uses a type-based selector. With mtcars it moves nothing, because every column is numeric, which also shows that relocate() with an empty selection leaves the order unchanged.

select(): choose and order in one step

select() returns only the columns you name, in the order you name them. To move some columns to the front and keep the rest, add everything() at the end. Without it, unnamed columns are removed. That is the most common surprise when people use select() purely to reorder.

# Move hp and wt first, keep all other columns
mtcars %>% select(hp, wt, everything())

# Keep ONLY hp and wt (other columns are dropped)
mtcars %>% select(hp, wt)

# Order by a character vector of names
col_order <- c("wt", "mpg", "cyl")
mtcars %>% select(all_of(col_order), everything())

# Alphabetical column order
mtcars %>% select(all_of(sort(names(mtcars))))

# Reverse the column order
mtcars %>% select(all_of(rev(names(mtcars))))

all_of() fails with an error if any name in the vector is missing. any_of() quietly skips missing names. Use all_of() when a missing column means something has gone wrong upstream. Use any_of() when the vector is a preferred order that may not fully apply to every input.

Quick comparison

Aspect | relocate() | select() with everything() | select() without everything()
Drops unnamed columns | No | No | Yes
Typical use | Move a few columns | Put a defined set first | Keep and order a subset
Placement control | .before / .after | Order of arguments | Order of arguments
Can rename at the same time | Yes, with new = old | Yes, with new = old | Yes, with new = old

Base R equivalent

new_order <- c("hp", "wt", setdiff(names(mtcars), c("hp", "wt")))
mtcars2 <- mtcars[, new_order]

This gives the same column order without dplyr. It is more verbose, but it has no dependency, which can matter in minimal scripts or packages.

Will the result stay usable after export or sharing?

Because the new order is stored in the object, writing it out with functions such as write.csv() or readr's write_csv() produces a file whose columns appear in that order. Anyone who opens the file in a spreadsheet or text editor sees the reordered layout. The values themselves are unchanged, so copying cells or rows between tools is not affected by the reorder.

Check these points before sharing or reusing reordered data:

  • Positional references: scripts, spreadsheet formulas or import mappings that point to a column number will read a different column after reordering. Prefer name-based references where possible.
  • Grouped data: select() on a grouped data frame keeps the grouping variables even if you do not name them, and reports that it added them. So the result can contain more columns than you listed.
  • Database-backed tables: with dbplyr, column selection and ordering are generally translated into the SQL column list. Behavior for a specific backend should be confirmed against that backend's documentation or a direct test. This article has no verified results for any particular database.
  • Accessibility of rendered tables: when a data frame is turned into an HTML or document table, column order becomes the reading order for people using screen readers and keyboard navigation. Putting identifying columns, such as a name or ID, first generally makes rows easier to understand when cells are announced one at a time. The table still needs proper header markup from whatever tool renders it. Reordering alone does not provide that.
  • Search and editing: reordering does not alter text values, so searching within cells, filtering and editing behave as before.

If a recipient or system depends on the original layout, export a copy in that layout as well, or document the new column order alongside the file. That is the practical counterpart of keeping a plain-text version: the data is identical, but the expected structure is preserved.

Evidence gaps

No version-specific details, performance measurements or tool-specific test results were provided for this review. Function behavior described here reflects dplyr's documented interface. If you rely on a specific dplyr release, a database backend, or a particular spreadsheet or assistive technology, confirm the behavior in that environment rather than assuming it from a single successful run elsewhere.