How can I drop all columns except specific ones in R?

How can I drop all columns except specific ones in R?

The process of dropping all columns except specific ones in R refers to the action of removing unwanted columns from a dataset while retaining only the desired ones. This can be achieved by using the subset function in R, which allows for the selection of specific columns to be kept and all others to be dropped. This method is particularly useful when working with large datasets, as it reduces the amount of unnecessary information and can improve the efficiency of data analysis. By specifying the desired columns to be kept, this process allows for a more streamlined and targeted approach to data manipulation in R.

R: 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 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.

Cite this article

stats writer (2024). How can I drop all columns except specific ones in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-except-specific-ones-in-r/

stats writer. "How can I drop all columns except specific ones in R?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-except-specific-ones-in-r/.

stats writer. "How can I drop all columns except specific ones in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-except-specific-ones-in-r/.

stats writer (2024) 'How can I drop all columns except specific ones in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-all-columns-except-specific-ones-in-r/.

[1] stats writer, "How can I drop all columns except specific ones in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I drop all columns except specific ones in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top