Guru's Verification engine ensures consistency, confidence, and trust in the knowledge your organization shares. Learn more.

R | Dropping column variables

Dropping columns in R dataframes can be done in a myriad of ways. This card will describe a few the more basic options.

One simple way to do so in core R is through the subset function (note the lack of quotes):

subset(iris, select = -c(Sepal.Width, Petal.Length))

If you need to drop columns whose names are stored in a variable, it's possible to do this in best R, but it's rather clunky, and probably easiest to just use the dplyr package's select function:

cols_to_drop <- c("Sepal.Width","Petal.Length")# Base R:iris[,!(names(iris) %in% cols_to_drop]# dplyr:dplyr::select(!all_of(cols_to_drop))

See resources for more advanced ways of dropping columns, though note that even that's not an exhaustive list.

Resources:

You must have Author or Collection Owner permission to create Guru Cards. Contact your team's Guru admins to use this template.