Skip to contents

Return column in data by types

Usage

select_by_class(df, class, return_tbl = TRUE)

Arguments

df

a data.frame or tibble

return_tbl

logical, return tibble (TRUE) or named vector (FALSE)

type

type of column to return One of:

  • "log": logical

  • "dbl": double

  • "int": integer

  • "chr": character

  • "fct": factor

  • "ord": factor (ordered)

  • "list": list

Value

named vector or tibble of columns matching the type (empty vector or tibble if no columns of specified type exist)

Examples

select_by_class(dplyr::starwars, class = "chr")
#> # A tibble: 87 × 8
#>    name           hair_color skin_color eye_color sex   gender homeworld species
#>    <chr>          <chr>      <chr>      <chr>     <chr> <chr>  <chr>     <chr>  
#>  1 Luke Skywalker blond      fair       blue      male  mascu… Tatooine  Human  
#>  2 C-3PO          NA         gold       yellow    none  mascu… Tatooine  Droid  
#>  3 R2-D2          NA         white, bl… red       none  mascu… Naboo     Droid  
#>  4 Darth Vader    none       white      yellow    male  mascu… Tatooine  Human  
#>  5 Leia Organa    brown      light      brown     fema… femin… Alderaan  Human  
#>  6 Owen Lars      brown, gr… light      blue      male  mascu… Tatooine  Human  
#>  7 Beru Whitesun… brown      light      blue      fema… femin… Tatooine  Human  
#>  8 R5-D4          NA         white, red red       none  mascu… Tatooine  Droid  
#>  9 Biggs Darklig… black      light      brown     male  mascu… Tatooine  Human  
#> 10 Obi-Wan Kenobi auburn, w… fair       blue-gray male  mascu… Stewjon   Human  
#> # ℹ 77 more rows
select_by_class(dplyr::starwars, class = "chr", return_tbl = FALSE)
#>         name   hair_color   skin_color    eye_color          sex       gender 
#>       "name" "hair_color" "skin_color"  "eye_color"        "sex"     "gender" 
#>    homeworld      species 
#>  "homeworld"    "species" 
select_by_class(dplyr::starwars, class = "list")
#> # A tibble: 87 × 3
#>    films     vehicles  starships
#>    <list>    <list>    <list>   
#>  1 <chr [5]> <chr [2]> <chr [2]>
#>  2 <chr [6]> <chr [0]> <chr [0]>
#>  3 <chr [7]> <chr [0]> <chr [0]>
#>  4 <chr [4]> <chr [0]> <chr [1]>
#>  5 <chr [5]> <chr [1]> <chr [0]>
#>  6 <chr [3]> <chr [0]> <chr [0]>
#>  7 <chr [3]> <chr [0]> <chr [0]>
#>  8 <chr [1]> <chr [0]> <chr [0]>
#>  9 <chr [1]> <chr [0]> <chr [1]>
#> 10 <chr [6]> <chr [1]> <chr [5]>
#> # ℹ 77 more rows
select_by_class(dplyr::starwars, class = "list", return_tbl = FALSE)
#>       films    vehicles   starships 
#>     "films"  "vehicles" "starships" 
select_by_class(tidyr::fish_encounters, "chr")
#> # A tibble: 0 × 0
select_by_class(tidyr::fish_encounters, "chr", return_tbl = FALSE)
#> named character(0)