How to delete multiple columns in R?

To delete multiple columns in R, you can use the subset function and pass in the columns you want to delete as arguments. This will return a new data frame with all the columns you specified removed. You can also use the select function to remove multiple columns, passing in the column names you want to keep as arguments.


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.

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