how do i extract specific columns from a data frame in r?

In R, you can extract specific columns from a data frame by specifying the column names within square brackets after the name of the data frame. For example, if you wanted to extract the first two columns from a data frame called “data”, you could specify the command as “data[,c(1,2)]”. This will return a subset of the data frame with just the specified columns. You can also use the “$” operator to extract columns by name. For example, “data$column1” will return the values from the column named “column1” in the data frame “data”.


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

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.

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

x