R: How to drop all columns except some in R

R provides the subset() function which can be used to drop all columns in a data frame except those that are specified. This is done by specifying the column names to be kept in the select argument of the subset() function. The subset() function returns a new data frame with only the specified columns, allowing the user to quickly drop all columns except those they wish to keep.


You can use the following methods to drop all columns except specific ones from a data frame in R:

Method 1: Use Base R

df <- df[c('col2', 'col6')]

Method 2: Use dplyr

library(dplyr)

df <- df %>% select(col2, col6)

Both methods drop all columns in the data frame except the columns called col2 and col6.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 points=c(18, 22, 19, 14, 14, 11, 20, 28),
                 assists=c(5, 7, 7, 9, 12, 9, 9, 4),
                 rebounds=c(11, 8, 10, 6, 6, 5, 9, 12),
                 steals=c(4, 3, 3, 2, 5, 4, 3, 8),
                 blocks=c(1, 0, 0, 3, 2, 2, 1, 5))

#view data frame
df

  team points assists rebounds steals blocks
1    A     18       5       11      4      1
2    B     22       7        8      3      0
3    C     19       7       10      3      0
4    D     14       9        6      2      3
5    E     14      12        6      5      2
6    F     11       9        5      4      2
7    G     20       9        9      3      1
8    H     28       4       12      8      5

Example 1: Drop All Columns Except Specific Ones Using Base R

We can use the following syntax to drop all columns in the data frame except the ones called points and blocks:

#drop all columns except points and blocks
df <- df[c('points', 'blocks')]

#view updated data frame
df

  points blocks
1     18      1
2     22      0
3     19      0
4     14      3
5     14      2
6     11      2
7     20      1
8     28      5

Notice that only the points and blocks columns remain.

All other columns have been dropped.

Example 2: Drop All Columns Except Specific Ones Using dplyr

We can also use the select() function from the package to drop all columns in the data frame except the ones called points and blocks:

library(dplyr)

#drop all columns except points and blocks 
df <- df %>% select(points, blocks)

#view updated data frame
df

  points blocks
1     18      1
2     22      0
3     19      0
4     14      3
5     14      2
6     11      2
7     20      1
8     28      5

Notice that only the points and blocks columns remain.

The following tutorials explain how to perform other common tasks in R:

x