Selecting by name range

select(my_data_frame, column_one, column_two, ...)
select(my_data_frame, new_column_name = current_column, ...)
select(my_data_frame, column_start:column_end)
select(my_data_frame, index_one, index_two, ...)
select(my_data_frame, index_start:index_end)
View Interactive Version

When we use the select() function and define the columns we want to keep, dplyr does not actually use the name of the columns but the index of the columns in the data frame. This means, when we define the first three columns of the pres_results data frame, year, state and total_votes, dplyr converts these names to the index values 1, 2 and 3. We can therefore also use the name of the columns, apply the : operator and define ranges of columns, that we want to keep:

Input
select(pres_results, year:total_votes)
Output
# A tibble: 561 x 3
   year state total_votes
  <dbl> <chr>       <dbl>
1  1976 AK         123574
2  1976 AL        1182850
3  1976 AR         767535
# … with 558 more rows

What the year:total_votes does, can be translated to 1:3, which is simply creating a vector of numerical values from 1 to 3. Then, the select() function takes the pres_results data frame and outputs a subset of it, keeping only the first three columns.

Select columns from a data frame