Home ยป How to Extract Specific Columns from Data Frame in R

How to Extract Specific Columns from Data Frame in R

by Tutor Aspire

You can use the following methods to extract specific columns from a data frame in R:

Method 1: Extract Specific Columns Using Base R

df[c('col1', 'col3', 'col4')]

Method 2: Extract Specific Columns Using dplyr

library(dplyr)

df %>%
  select(col1, col3, col4)

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

#create data frame
df frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28),
                 steals=c(9, 12, 4, 7, 8))

#view data frame
df

  team points assists rebounds steals
1    A     99      33       30      9
2    B     90      28       28     12
3    C     86      31       24      4
4    D     88      39       24      7
5    E     95      34       28      8

Method 1: Extract Specific Columns Using Base R

The following code shows how to extract the team, assists, and rebounds columns using base R:

#select 'team', 'assists' and 'rebounds' columns
df[c('team', 'assists', 'rebounds')]

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

Notice that each of the columns we specified have been extracted from the data frame.

Also note that you can extract these columns by index position as well:

#select columns in index positions 1, 3 and 4
df[c(1, 3, 4)]

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

This syntax extracts the columns in column index positions 1, 3 and 4.

Method 2: Extract Specific Columns Using dplyr

The following code shows how to extract the team, assists, and rebounds columns using the select() function from the dplyr package:

library(dplyr)

#select 'team', 'assists' and 'rebounds' columns
df %>%
  select(team, assists, rebounds)

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

Notice that each of the columns we specified have been extracted from the data frame.

Also note that you can extract these columns by index position as well:

library(dplyr)

#select 'team', 'assists' and 'rebounds' columns
df %>%
  select(1, 3, 4)

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

This syntax extracts the columns in column index positions 1, 3 and 4.

Additional Resources

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

How to Select Only Numeric Columns in R
How to Delete Multiple Columns in R
How to Reorder Columns in R

You may also like