Remove columns with dplyr
How to drop columns from an R data frame with dplyr's select(), what it changes, common pitfalls, and when base R is the simpler choice.
Dropping columns is one of the most common cleanup steps in R, and dplyr handles it mainly through select(). This review covers the main ways to remove columns with dplyr, what each call actually changes, where it can surprise you, and when base R is the simpler choice. No test data or benchmarks were supplied for this article, so it relies on dplyr's documented behavior rather than on measurements.
Is dplyr the right tool for removing columns?
For most analysis scripts, yes. The main benefit is readability. A call such as select(-notes) states its intent clearly, fits naturally into a pipe chain, and can target columns by name, name pattern or data type without manual indexing. The main tradeoff is that you add a package dependency and some extra rules, mostly around grouped data and missing column names.
Base R is often the better choice in these cases:
- You are writing a package or a minimal script and want to avoid dependencies.
- You only need to drop one column in place, where df$col <- NULL is shorter and has no extra behavior to learn.
- The code will be maintained by people who do not use the tidyverse.
Basic ways to drop columns with select()
Inside select(), a minus sign or the ! operator excludes columns. Every column you do not exclude is kept in its original order. The examples below use a small illustrative data frame.
library(dplyr)
df <- data.frame(
id = 1:3,
name = c("a", "b", "c"),
score = c(10, 20, 30),
notes = c("x", "y", "z")
)
# Drop one column
df %>% select(-notes)
# Drop several columns
df %>% select(-c(score, notes))
df %>% select(!c(score, notes)) # equivalent negation with !
# Drop by position (fragile if column order changes)
df %>% select(-1)
df %>% select(-(2:3))
Column names that contain spaces or start with a number must be wrapped in backticks, for example select(-total score).
Dropping columns by pattern or type
Tidyselect helpers let you remove groups of columns without listing each one. This is a real advantage over base R when a dataset contains many similarly named or similarly typed fields.
# Drop columns whose names start with, end with, or contain text
df %>% select(-starts_with("tmp_"))
df %>% select(-ends_with("_id"))
df %>% select(-contains("note"))
# Drop columns by type
df %>% select(-where(is.character))
Pattern helpers match whatever is present at run time. If new columns arrive later with matching names, they will be dropped too. That is convenient but worth knowing before you use these helpers on data whose schema changes.
Dropping columns named in a character vector
When the column names come from a variable, wrap them in all_of() or any_of(). The difference matters when a name might not exist.
drop_these <- c("score", "notes", "not_a_column")
# Errors, because one name is missing
df %>% select(-all_of(drop_these))
# Silently skips names that are not present
df %>% select(-any_of(drop_these))
Use all_of() if a missing column means something has gone wrong upstream. Use any_of() when removal is best-effort, such as cleaning files that do not all share the same columns.
What select() actually changes
select() returns a new data frame that contains only the remaining columns. It does not change the original object. A pipeline that drops columns has no lasting effect unless you assign the result.
df %>% select(-notes) # prints a result; df still has notes
df <- df %>% select(-notes) # df now lacks notes
Rows and values are not touched. Only the set of columns changes. Because select() also reorders and renames, a call like select(-notes, new_name = name) does more than remove a column, so read mixed calls carefully.
Another option is mutate(notes = NULL), which also removes a column. It is mostly useful when you are already inside a mutate() call. For plain removal, select() states the intent more directly.
Grouped data: why a column may not disappear
On a grouped data frame, select() keeps the grouping variables even if you try to drop them, and prints a message that it is adding the missing grouping variables. To actually remove a grouping column, call ungroup() first.
df %>%
group_by(name) %>%
ungroup() %>%
select(-name)
Comparison with base R
| Task | dplyr | base R |
| Drop one column | df %>% select(-notes) | df$notes <- NULL |
| Drop several columns | df %>% select(-c(score, notes)) | df[, !(names(df) %in% c("score", "notes"))] |
| Drop with a select argument | not applicable | subset(df, select = -c(score, notes)) |
| Drop by name pattern | select(-starts_with("tmp_")) | df[, !startsWith(names(df), "tmp_")] |
| Tolerate missing names | select(-any_of(x)) | df[, setdiff(names(df), x)] |
In base R, single-bracket indexing that leaves only one column returns a vector by default. Add drop = FALSE to keep a data frame. select() always returns a data frame, which is one practical reason people choose it inside longer pipelines.
Will the result stay usable when shared?
Once you write a trimmed data frame to a file, for example with write.csv(), the dropped columns are gone from that file. Anyone who needs them must go back to the source data. Keep the original data, and treat the script as the record of what was removed.
For code that others will read or rerun, prefer explicit names over positions. select(-(2:3)) silently removes the wrong columns if the column order changes. select(-c(score, notes)) either does the right thing or fails loudly. The same reasoning favors all_of() over any_of() whenever a missing column should be treated as an error.
If the data frame is a remote table, such as a database table accessed through dbplyr, select() is translated into a query rather than run in memory. Remote backends are outside the scope of this review. Check your backend's documentation before assuming every helper behaves identically there.
Bottom line
For interactive analysis and pipe-based scripts, select() with a minus sign or ! is a clear and flexible way to remove columns from a data frame. Its pattern and type helpers are the main advantage over base R. The things to watch are assigning the result, ungrouping before dropping grouping variables, and choosing between all_of() and any_of() on purpose. For a single in-place removal, or for dependency-free code, df$col <- NULL remains the simpler option.