Select table columns with tidyverse and dplyr

How dplyr's select() keeps, drops, renames and reorders data frame columns, what it does not change, and when base R subsetting is the simpler choice.

In the tidyverse, column selection is handled by dplyr's select() function. Loading the tidyverse attaches dplyr, so "tidyverse select" and "dplyr::select" refer to the same function. This review covers what select() changes in a table, what it leaves alone, how readable the resulting code and output stay when shared, and when a base R approach is the better fit.

Is select() suitable for your intended use?

For interactive analysis and pipelines that already use dplyr, select() is a strong default. Its main benefit is expressive column selection: you can name columns directly, use ranges, drop columns, rename them in the same step, and match columns by name pattern or by type. The resulting code tends to read close to a plain description of the intent.

The decisive tradeoff is dependency and evaluation style. select() relies on the dplyr and tidyselect packages and on non-standard evaluation, where bare column names are interpreted inside the data frame rather than as ordinary R objects. This is convenient at the console but needs extra care inside functions and when column names arrive as character vectors.

Base R subsetting, such as df[, c("a", "b")] or df[c("a", "b")], is often preferable when you are writing a package or script that should avoid extra dependencies, when column names are already held in a character vector and no pattern matching is needed, or when collaborators do not use the tidyverse. No performance measurements were supplied for this review, so no claim is made about speed differences between the two approaches.

What select() actually changes

select() changes which columns are present and in what order. It can also change column names when you use the new_name = old_name form. It does not change row count, row order or the values stored in the columns it keeps.

It also does not modify the original object. select() returns a new data frame; the input stays as it was unless you assign the result back to the same name. This distinction matters when judging whether a step is reversible: dropping a column in a pipeline is only permanent if you overwrite the source object.

The word "select" can be ambiguous. In dplyr it means choosing columns. Choosing rows is done with filter() or slice(). Extracting a single column as a plain vector is done with pull(). Keeping every column while only renaming uses rename(), and moving columns without dropping any uses relocate().

Core selection patterns

The examples below use the mtcars and iris datasets that ship with R. The mtcars columns, in order, are mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear and carb. The comments describe the resulting column names.

library(dplyr)

# By name: mpg, cyl, hp
select(mtcars, mpg, cyl, hp)

# By range: mpg, cyl, disp, hp
select(mtcars, mpg:hp)

# Drop a range: mpg, cyl, disp, hp
select(mtcars, !(drat:carb))

# Rename while selecting: miles_per_gallon, cyl
select(mtcars, miles_per_gallon = mpg, cyl)

# Move a column to the front, keep the rest
select(iris, Species, everything())

Negation can be written with ! or with a minus sign. Current dplyr documentation favors ! for consistency with the other boolean operators described below.

Selection helpers

select() uses the tidyselect language, which provides helper functions for matching columns without listing each one. The same helpers work in other tidyverse functions that take column selections, such as the cols argument in tidyr's pivot_longer().

Helper | What it matches | Example
starts_with() | Names beginning with a prefix | starts_with("d") on mtcars gives disp, drat
ends_with() | Names ending with a suffix | ends_with("t") on mtcars gives drat, wt
contains() | Names containing a literal string | contains("Length") on iris
matches() | Names matching a regular expression | matches("^S") on iris
num_range() | Numbered names such as x1, x2, x3 | num_range("x", 1:3)
where() | Columns for which a function returns TRUE | where(is.numeric) on iris drops Species
everything() | All remaining columns | Used for reordering
last_col() | The final column | last_col() on mtcars gives carb
all_of() | Names in a character vector; errors if any are missing | all_of(c("mpg", "cyl"))
any_of() | Names in a character vector; silently skips missing ones | any_of(c("mpg", "not_here")) gives mpg

Selections can be combined with & (intersection), | (union) and ! (complement). For example, where(is.numeric) & starts_with("Sepal") on iris keeps only the two Sepal columns.

Behavior that can surprise readers

  • Column order follows the order of your selection, not the original table order, so select(mtcars, hp, mpg) puts hp first.
  • On a grouped data frame, grouping columns are kept even if you do not select them, and dplyr prints a message saying it added the missing grouping variables.
  • When column names are stored in an external character vector, wrap the vector in all_of() or any_of(). Passing the vector directly is ambiguous because a column with the same name as the vector could be picked up instead.
  • If another loaded package also exports a function called select(), such as MASS, whichever package was attached last masks the other. Writing dplyr::select() avoids this conflict, which is one common reason people search for the namespaced form.
  • Column names that contain spaces or start with a number must be wrapped in backticks when written as bare names.
# Grouping variable cyl is added automatically
mtcars |>
  group_by(cyl) |>
  select(mpg)

# Safe use of a character vector
wanted <- c("mpg", "hp")
select(mtcars, all_of(wanted))

# Explicit namespace avoids masking
dplyr::select(mtcars, mpg, cyl)

Will the result stay readable and usable when shared?

Because select() only removes, reorders or renames columns, the data it keeps is unchanged and remains fully usable for further analysis or export. The main readability risk is loss of context: dropping an identifier, unit or label column can make the remaining numbers harder to interpret once the table leaves R. When sharing a subset, check that each kept column still makes sense without the dropped ones.

Renaming inside select() can improve readability for an outside audience, but it also breaks the link to the original source column names. If others need to trace values back to the source data, keep a note of the mapping or perform renaming as a separate, clearly visible step with rename().

Printed console output is not a reliable way to share results. Tibbles truncate wide output and may hide columns or rows on screen, so copying from the console can give an incomplete picture. Writing the selected table to a file, for example a CSV, preserves every kept column and row in plain text that other tools can read.

For sharing code rather than data, helper-based selections such as starts_with() or where() depend on the column names and types present at run time. If the input data gains or loses columns, the same code can return a different set of columns. Where a stable, auditable result matters, listing columns explicitly or using all_of() with a fixed vector is the more predictable choice.

When select() is applied to a remote database table through dbplyr, it is translated to a SQL SELECT clause rather than run in R memory. Not every R expression used inside where() can be translated, so type-based selection may behave differently on remote tables. No scoped test results for specific database backends were supplied for this review.

Evidence gaps

This review relies on documented dplyr and tidyselect behavior and on the structure of datasets bundled with R. No benchmark figures, version-specific test results or assistive technology evaluations were supplied, so this article makes no claims about relative speed, memory use or how exported tables are read by screen readers or other assistive software.

Bottom line

Use select() when you work in the tidyverse and want readable, flexible column selection with pattern and type matching. Watch for grouping columns being retained, function masking from other packages and helper-based selections that shift when the data changes. Choose base R subsetting when avoiding dependencies matters or when you simply need a fixed set of named columns.