How to Drop Data Frame Columns by Name in R

How to drop data frame columns by name in R

This tutorial explains how to drop data frame columns by name in R.

Drop Columns by Name

Suppose we have the following data frame with four columns:

data <- data.frame(team = c('Mavericks', 'Spurs', 'Lakers'),
                   city = c('Dallas', 'San Antonio', 'Los Angeles'),
                   avgPoints = c(103.2, 99.4, 105.2),
                   avgRebounds = c(43.2, 40.5, 33.3))
data
#       team        city avgPoints avgRebounds
#1 Mavericks      Dallas     103.2        43.2
#2     Spurs San Antonio      99.4        40.5
#3    Lakers Los Angeles     105.2        33.3

We can use the following syntax to create a subsetted data frame by dropping specific columns of the original data frame by name:

subset(data, select = -c(drop_column1, drop_column2, …))

For example, we can create a subsetted data frame by dropping the columns avgPoints and avgRebounds: 

#drop avgPoints and avgRebounds
sub_data <- subset(data, select = -c(avgPoints, avgRebounds))
sub_data

#       team     city
#1 Mavericks   Dallas
#2 Spurs  San Antonio
#3 Lakers Los Angeles

Alternatively, we can use the following syntax to create a subsetted data frame by by simply not including specific columns of the original data frame:

subset(data, select = c(keep_column1, keep_column2, …))

For example, we can create the same subsetted data frame as above by simply not including avgPoints and avgRebounds in the select argument:

#drop avgPoints and avgRebounds
sub_data <- subset(data, select = c(team, city))
sub_data

#       team     city
#1 Mavericks   Dallas
#2 Spurs  San Antonio
#3 Lakers Los Angeles

Leave a Reply

Your email address will not be published. Required fields are marked *