R: How to Drop All Columns Except Specific Ones


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 dplyr 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.

This matches the results from the previous example.

Additional Resources

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

How to Drop Columns if Name Contains Specific String in R
How to Drop Multiple Columns Using dplyr
How to Drop Columns with NA Values in R

Leave a Reply

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