Remove columns by name in R
How to drop data frame columns by name in R with base R, dplyr and data.table, what each approach changes, and the pitfalls that silently break results.
Dropping columns by name is one of the most common cleanup steps in R. There are several ways to do it, and they differ in what they return, whether they modify the original object, and how they handle names that do not exist. This review covers the main base R, dplyr and data.table approaches, the cases where each one fits, and the pitfalls that can change your results without any error.
Which approach should you use?
For most scripts, choose by the tools your project already uses. If you work in the tidyverse, dplyr::select() with a minus sign is the most readable option and fits into pipelines. If you want no package dependencies, list-style indexing with a character vector of names is the safest base R pattern. If you work with large data.table objects and want to avoid copying, the := NULL form removes columns in place. The decisive tradeoff is between returning a new object, which is predictable and easy to reason about, and modifying the existing one, which avoids a copy but can surprise you if other code still refers to the same table.
Base R options
Assigning NULL to a column removes it from the data frame. This works for one column or several at once.
df$temp <- NULL
df[["temp"]] <- NULL
df[c("temp", "notes")] <- NULL
To keep the original intact and create a new data frame without certain columns, index with a logical or character vector built from names(). Indexing a data frame with a single bracket and no comma treats it like a list and always returns a data frame.
drop <- c("temp", "notes")
clean <- df[!(names(df) %in% drop)]
# Equivalent, using setdiff()
clean <- df[setdiff(names(df), drop)]
If you prefer the row-and-column form df[, ...], add drop = FALSE. Without it, a plain data frame collapses to a vector when only one column remains.
clean <- df[, !(names(df) %in% drop), drop = FALSE]
The subset() function also accepts unquoted names with a minus sign. Its own documentation describes it as a convenience for interactive use and advises caution in programming, because it relies on non-standard evaluation.
clean <- subset(df, select = -c(temp, notes))
dplyr
In dplyr, select() with a minus sign excludes columns. You can list bare names, use helpers that match patterns, or pass a character vector through all_of() or any_of().
library(dplyr)
clean <- df |> select(-temp, -notes)
clean <- df |> select(-c(temp, notes))
clean <- df |> select(-starts_with("tmp_"))
drop <- c("temp", "notes")
clean <- df |> select(-all_of(drop)) # errors if a name is missing
clean <- df |> select(-any_of(drop)) # skips names that are missing
The choice between all_of() and any_of() is the main practical decision. all_of() stops with an error when a name is absent, which catches typos and unexpected input. any_of() quietly ignores absent names, which is useful when the same code runs on files that do not all share the same columns.
data.table
For data.table objects, assigning NULL with := removes columns by reference. The table is changed in place and no modified copy is returned for you to store.
library(data.table)
dt[, c("temp", "notes") := NULL]
# Return a new table instead, leaving dt unchanged
clean <- dt[, !c("temp", "notes")]
Because := modifies the object directly, any other variable that points to the same data.table will also lose those columns. Use copy() first if you need to keep the original.
What each method actually changes
| Method | Returns | Modifies original | Missing name behavior |
| df$x <- NULL or df[c(...)] <- NULL | The same data frame, reassigned | Yes, the variable is updated | Varies; check your case |
| df[!(names(df) %in% drop)] | New data frame | No | Ignored silently |
| df[setdiff(names(df), drop)] | New data frame | No | Ignored silently |
| subset(df, select = -c(...)) | New data frame | No | Error |
| select(-all_of(drop)) | New data frame or tibble | No | Error |
| select(-any_of(drop)) | New data frame or tibble | No | Ignored silently |
| dt[, c(...) := NULL] | The data.table, changed by reference | Yes, in place | Error |
Removing a column only removes that variable. Row order, row count and the values in the remaining columns stay the same with every method above. Column order of the remaining columns is also preserved.
Pitfalls that change results without an error
The most damaging mistake is negative indexing with which(). If none of the names match, which() returns an empty integer vector, and negating it selects zero columns. You end up with a data frame that has rows but no columns, and R does not warn you.
# Risky: if no names match, every column is dropped
clean <- df[, -which(names(df) %in% drop)]
# Safe: logical indexing keeps all columns when nothing matches
clean <- df[!(names(df) %in% drop)]
- Single-column collapse: df[, cols] on a plain data frame returns a vector when one column is left. Tibbles do not do this by default.
- Silent misses: %in% and setdiff() ignore misspelled names, so a typo leaves the column in place. Use all_of() or an explicit check when the columns must exist.
- Non-syntactic names: names with spaces or symbols need backticks in dplyr and subset(), for example select(-unit price). Character vectors in base R need no special handling.
- Reference semantics: data.table's := changes every variable that refers to the same table.
If you need a guard in base R, compare the requested names to the actual ones before dropping.
missing <- setdiff(drop, names(df))
if (length(missing) > 0) stop("Columns not found: ", paste(missing, collapse = ", "))
Will the code stay readable and portable?
Code that uses a character vector of names, such as df[setdiff(names(df), drop)] or select(-all_of(drop)), is the easiest to reuse: the list of columns can come from a variable, a function argument or a config file. Bare-name forms like select(-temp) or subset(df, select = -temp) read well in interactive analysis but are harder to use inside functions without extra tidy evaluation syntax. Base R patterns run anywhere R does; the dplyr and data.table versions require those packages to be installed, which matters when sharing scripts.
Gaps in this review
No benchmark data was supplied, so this review makes no claims about speed or memory use beyond the documented difference that data.table's := avoids creating a modified copy. Behavior described here reflects documented package semantics; details such as error message wording can differ between package versions, so check the documentation for the versions you use.