dplyr - how to select columns of certain type

As of dplyr 1.0.0 *_if functions and friends have been superseded. Using the selection helper where from tidyselect is now recommended.

https://dplyr.tidyverse.org/reference/select.html
https://tidyselect.r-lib.org/reference/where.html

library(dplyr)

starwars %>% 
    select(where(is.character))
#> # A tibble: 87 x 8
#>    name        hair_color   skin_color eye_color sex   gender  homeworld species
#>    <chr>       <chr>        <chr>      <chr>     <chr> <chr>   <chr>     <chr>  
#>  1 Luke Skywa~ blond        fair       blue      male  mascul~ Tatooine  Human  
#>  2 C-3PO       <NA>         gold       yellow    none  mascul~ Tatooine  Droid  
#>  3 R2-D2       <NA>         white, bl~ red       none  mascul~ Naboo     Droid  
#>  4 Darth Vader none         white      yellow    male  mascul~ Tatooine  Human  
#>  5 Leia Organa brown        light      brown     fema~ femini~ Alderaan  Human  
#>  6 Owen Lars   brown, grey  light      blue      male  mascul~ Tatooine  Human  
#>  7 Beru White~ brown        light      blue      fema~ femini~ Tatooine  Human  
#>  8 R5-D4       <NA>         white, red red       none  mascul~ Tatooine  Droid  
#>  9 Biggs Dark~ black        light      brown     male  mascul~ Tatooine  Human  
#> 10 Obi-Wan Ke~ auburn, whi~ fair       blue-gray male  mascul~ Stewjon   Human  
#> # ... with 77 more rows

Created on 2020-06-02 by the reprex package (v0.3.0)


Dplyr 0.5 has the select_if() that allows you to write select_if(is.character)

Tags:

R

Dplyr