Home ยป How to Select Columns by Index Using dplyr

How to Select Columns by Index Using dplyr

by Tutor Aspire

You can use the following basic syntax in dplyr to select data frame columns by index position:

#select columns in specific index positions
df %>%
  select(1, 4, 5)

#exclude columns in specific index positions
df %>%
  select(-c(1,2))

The following examples show how to use this syntax in practice with the following data frame:

#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),
                 blocks=c(14, 19, 22, 18, 15))

#view data frame
df

  team points assists rebounds blocks
1    A     99      33       30     14
2    B     90      28       28     19
3    C     86      31       24     22
4    D     88      39       24     18
5    E     95      34       28     15

Example 1: Select Columns in Specific Index Positions

The following code shows how to select columns in specific index positions:

library(dplyr)

#select columns in position 1, 4, and 5
df %>%
  select(1, 4, 5)

  team rebounds blocks
1    A       30     14
2    B       28     19
3    C       24     22
4    D       24     18
5    E       28     15

Example 2: Select Columns in Range

The following code shows how to select columns in a range:

library(dplyr)

#select columns in position 2 through 4
df %>%
  select(2:4)

  points assists rebounds
1     99      33       30
2     90      28       28
3     86      31       24
4     88      39       24
5     95      34       28

Example 3: Exclude Specific Columns

The following code shows how to exclude specific columns based on index position:

library(dplyr)

#select all columns except those in position 1 and 2
df %>%
  select(-c(1, 2))

  assists rebounds blocks
1      33       30     14
2      28       28     19
3      31       24     22
4      39       24     18
5      34       28     15

Notice that the first and second column are excluded.

Additional Resources

The following tutorials explain how to perform other common functions in dplyr:

How to Select Columns by Name Using dplyr
How to Filter Rows that Contain a Certain String Using dplyr
How to Select the First Row by Group Using dplyr
How to Replace NA with Zero in dplyr

You may also like