Skip to contents

Return column in data by class()

Usage

select_column_class(df, class)

Arguments

df

a data.frame or tibble

class

class of column to return. One of:

  • "log": logical

  • "dbl": double

  • "int": integer

  • "chr": character

  • "fct": factor

  • "list": list

Value

tibble of columns matching class (empty tibble if no columns of specified type exist)

Examples

require(dplyr)
select_column_class(df = 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_column_class(df = dplyr::starwars, class = c("chr", "list"))
#> # A tibble: 87 × 11
#>    name     hair_color skin_color eye_color sex   gender homeworld species films
#>    <chr>    <chr>      <chr>      <chr>     <chr> <chr>  <chr>     <chr>   <lis>
#>  1 Luke Sk… blond      fair       blue      male  mascu… Tatooine  Human   <chr>
#>  2 C-3PO    NA         gold       yellow    none  mascu… Tatooine  Droid   <chr>
#>  3 R2-D2    NA         white, bl… red       none  mascu… Naboo     Droid   <chr>
#>  4 Darth V… none       white      yellow    male  mascu… Tatooine  Human   <chr>
#>  5 Leia Or… brown      light      brown     fema… femin… Alderaan  Human   <chr>
#>  6 Owen La… brown, gr… light      blue      male  mascu… Tatooine  Human   <chr>
#>  7 Beru Wh… brown      light      blue      fema… femin… Tatooine  Human   <chr>
#>  8 R5-D4    NA         white, red red       none  mascu… Tatooine  Droid   <chr>
#>  9 Biggs D… black      light      brown     male  mascu… Tatooine  Human   <chr>
#> 10 Obi-Wan… auburn, w… fair       blue-gray male  mascu… Stewjon   Human   <chr>
#> # ℹ 77 more rows
#> # ℹ 2 more variables: vehicles <list>, starships <list>
select_column_class(df = mtcars, class = "chr")
#> # A tibble: 0 × 0
select_column_class(df = mtcars, class = c("chr", "list"))
#> # A tibble: 0 × 0