How to Drop Columns from Data Frame in R (With Examples)

Dropping columns from a data frame in R is a simple process. Through the use of the select() and the – operator, you can easily drop multiple columns from a data frame in R. This can be done by passing a vector of column names to the select() function, or by using the – operator to remove the columns from the data frame. Additionally, the select() and drop_na() functions can be used to remove columns with blank or NA values. To further illustrate this, examples of dropping columns from a data frame in R are provided.


The easiest way to drop columns from a data frame in R is to use the subset() function, which uses the following basic syntax:

#remove columns var1 and var3
new_df <- subset(df, select = -c(var1, var3))

The following examples show how to use this function in practice with the following data frame:

#create data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 10, 12),
                 var4=c(14, 16, 22, 19, 18))

#view data frame
df

  var1 var2 var3 var4
1    1    7    3   14
2    3    7    3   16
3    3    8    6   22
4    4    3   10   19
5    5    2   12   18

Example 1: Drop Columns by Name

The following code shows how to drop columns from the data frame by name:

#remove columns var1 and var3
new_df <- subset(df, select = -c(var1, var3))

#view updated data frame
new_df

  var2 var4
1    7   14
2    7   16
3    8   22
4    3   19
5    2   18

Example 2: Drop Columns by Index

The following code shows how to drop columns from the data frame by index:

#remove first and fourth columns
new_df <- subset(df, select = -c(1, 4))

#view updated data frame
new_df

  var2 var3
1    7    3
2    7    3
3    8    6
4    3   10
5    2   12

Example 3: Drop Columns in List

The following code shows how to drop columns from the data frame that belong to a certain list:

#define list of columns to remove
remove_cols <- c('var1', 'var4')

#remove columns in list
new_df = subset(df, select = !(names(df) %in% remove_cols)) 

#view updated data frame
new_df

  var2 var3
1    7    3
2    7    3
3    8    6
4    3   10
5    2   12

Example 4: Drop Columns in Range

The following code shows how to drop columns from the data frame in a certain range:

#remove columns in range of 1 to 3
new_df = subset(df, select = -c(1:3)) 

#view updated data frame
new_df

  var4
1   14
2   16
3   22
4   19
5   18

x