dplyr::select()
   get_help() docs


Description

The select() function is part of the {dplyr} package, which is part of the {tidyverse}.

It is used to keep, remove, and/or reorder columns in tibbles (data frames).

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::select() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::select()

Conceptual Usage

tibble %>%
  select(single column to keep)

tibble %>%
  select(-single column to remove)

tibble %>% 
  select(columns, you, want, to, keep)

tibble %>% 
  select(-columns, -you, -want, -to, -remove)

Examples

The examples below use the carnivores dataset. Learn more about this dataset with get_help("carnivores").

# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# Keep only the columns name and genus from carnivores
carnivores %>% 
  select(name, genus)
## # A tibble: 9 × 2
##   name              genus       
##   <chr>             <fct>       
## 1 Arctic fox        Vulpes      
## 2 Cheetah           Acinonyx    
## 3 Dog               Canis       
## 4 Gray seal         Haliochoerus
## 5 Jaguar            Panthera    
## 6 Lion              Panthera    
## 7 Northern fur seal Callorhinus 
## 8 Red fox           Vulpes      
## 9 Tiger             Panthera


# Remove the column genus from carnivores
carnivores %>% 
  select(-genus)
## # A tibble: 9 × 3
##   name              awake brainwt
##   <chr>             <dbl>   <dbl>
## 1 Arctic fox         11.5  0.0445
## 2 Cheetah            11.9 NA     
## 3 Dog                13.9  0.07  
## 4 Gray seal          17.8  0.325 
## 5 Jaguar             13.6  0.157 
## 6 Lion               10.5 NA     
## 7 Northern fur seal  15.3 NA     
## 8 Red fox            14.2  0.0504
## 9 Tiger               8.2 NA


# Remove the columns genus and awake from carnivores
carnivores %>% 
  select(-genus, -awake)
## # A tibble: 9 × 2
##   name              brainwt
##   <chr>               <dbl>
## 1 Arctic fox         0.0445
## 2 Cheetah           NA     
## 3 Dog                0.07  
## 4 Gray seal          0.325 
## 5 Jaguar             0.157 
## 6 Lion              NA     
## 7 Northern fur seal NA     
## 8 Red fox            0.0504
## 9 Tiger             NA


# Reorder the columns in order: genus, name, brainwt, awake
carnivores %>%
  select(genus, name, brainwt, awake)
## # A tibble: 9 × 4
##   genus        name              brainwt awake
##   <fct>        <chr>               <dbl> <dbl>
## 1 Vulpes       Arctic fox         0.0445  11.5
## 2 Acinonyx     Cheetah           NA       11.9
## 3 Canis        Dog                0.07    13.9
## 4 Haliochoerus Gray seal          0.325   17.8
## 5 Panthera     Jaguar             0.157   13.6
## 6 Panthera     Lion              NA       10.5
## 7 Callorhinus  Northern fur seal NA       15.3
## 8 Vulpes       Red fox            0.0504  14.2
## 9 Panthera     Tiger             NA        8.2