How can I delete multiple columns in R?

To delete multiple columns in R, the user can use the “subset” function and specify the columns to be deleted. This function takes in the name of the data frame, followed by a comma, and then the columns to be removed within the square brackets. Additionally, the user can also use the “select” function from the dplyr package to remove columns by their names. Both of these methods allow for the efficient deletion of multiple columns in R.

Delete Multiple Columns in R (With Examples)


Often you may want to delete multiple columns at once from a data frame in R.

The easiest way to do this is with the following syntax:

df[ , c('column_name1', 'column_name2')] <- list(NULL)

For example, the following syntax shows how to delete columns 2 and 3 from a given data frame:

#create data frame
df <- data.frame(var1=c(1, 3, 2, 9, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 7))

#delete columns 2 and 3 from data frame
df[ , c('var2', 'var3')] <- list(NULL)

#view data frame
df

  var1 var4
1    1    1
2    3    1
3    2    2
4    9    8
5    5    7

We can also delete columns according to their index:

#create data frame
df <- data.frame(var1=c(1, 3, 2, 9, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 7))

#delete columns in position 2 and 3
df[ , c(2, 3)] <- list(NULL)

#view data frame
df

  var1 var4
1    1    1
2    3    1
3    2    2
4    9    8
5    5    7

And we can use the following syntax to delete all columns in a range:

#create data frame
df <- data.frame(var1=c(1, 3, 2, 9, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 7))

#delete columns in range 1 through 3
df[ , 1:3] <- list(NULL)

#view data frame
df

  var4
1    1
2    1
3    2
4    8
5    7

In general it’s recommended to delete columns by their name rather than their position simply because if you add or reorder columns then the positions could change.

By using column names, you ensure that you delete the correct columns regardless of their position.

Additional Resources

How to Loop Through Column Names in R
How to Combine Two Columns into One in R
How to Remove Outliers from Multiple Columns in R

x