How to select columns by name in R
· 2 min read · Updated March 14, 2026 · beginner
r selection dplyr data.table
Selecting specific columns is a frequent operation when working with data frames. Here are the main ways to do it.
With dplyr
The tidyverse approach uses select():
library(dplyr)
# Select single column
names_only <- df %>% select(name)
# Select multiple columns
selected <- df %>% select(name, salary, department)
# Select all columns between two names (inclusive)
range_select <- df %>% select(name:department)
# Select columns by pattern
starts_with_s <- df %>% select(starts_with("s"))
# Select columns by predicate
numeric_cols <- df %>% select(where(is.numeric))
Use ! to exclude columns:
# Exclude specific columns
without_id <- df %>% select(!id)
# Exclude columns matching a pattern
exclude_salary <- df %>% select(!starts_with("salary"))
With base R
Base R uses column indices or names:
# By column number
selected <- df[, c(1, 3, 5)]
# By name (character vector)
selected <- df[, c("name", "salary", "department")]
# Single column returns a vector
name_vec <- df[["name"]]
# Using which to match column names
cols_to_select <- which(names(df) %in% c("name", "salary"))
selected <- df[, cols_to_select]
With data.table
The data.table package provides fast column selection:
library(data.table)
dt <- as.data.table(df)
# Select by name
selected <- dt[, .(name, salary, department)]
# Exclude columns
without_id <- dt[, !c("id")]
# Select with pattern
starts_with_s <- dt[, .SD, .SDcols = patterns("^s")]
# Select numeric columns
numeric_cols <- dt[, .SD, .SDcols = is.numeric]
Helper Functions
| Function | Description | Example |
|---|---|---|
starts_with() | Columns starting with prefix | starts_with("sal") |
ends_with() | Columns ending with suffix | ends_with("date") |
contains() | Columns containing string | contains("id") |
matches() | Columns matching regex | matches("^x\\d+") |
everything() | All remaining columns | everything() |
where() | Columns where predicate is true | where(is.numeric) |
Reorder Columns
You can also reorder while selecting:
library(dplyr)
# Put important columns first, then everything else
reordered <- df %>% select(id, name, department, everything())