Skip to contents

select_tbl() displays frequency counts and percentages (i.e., count and percent) for multiple response variables, including binary variables (such as Unselected/Selected) and ordinal variables (such as responses ranging from strongly disagree to strongly agree), that share a common variable stem. A variable 'stem' is a shared naming pattern across related variables, often representing repeated measures of the same concept or a series of items measuring a single construct. Missing data are excluded using listwise deletion by default.

Usage

select_tbl(
  data,
  var_stem,
  escape_stem = FALSE,
  ignore_stem_case = FALSE,
  na_removal = "listwise",
  pivot = "longer",
  only = NULL,
  var_labels = NULL,
  ignore = NULL
)

Arguments

data

A data frame.

var_stem

A character string of a variable stem or the full name of a variable in data.

escape_stem

A logical value indicating whether to escape var_stem. Default is FALSE.

ignore_stem_case

A logical value indicating whether the search for columns matching the supplied var_stem is case-insensitive. Default is FALSE.

na_removal

A character string that specifies the method for handling missing values: pairwise or listwise. Defaults to listwise.

pivot

A character string that determines the format of the table. By default, longer returns the data in the long format. To receive the data in the wide format, specify wider.

only

A character string or vector of character strings of the types of summary data to return. Default is NULL, which returns both counts and percentages. To return only counts or percentages, use count or percent, respectively.

var_labels

An optional named character vector or list used to assign custom labels to variable names. Each element should be named and correspond to a variable in the returned table. If any element is unnamed or references a variable not returned in the table, all labels will be ignored and the table will be printed without them.

ignore

An optional vector of values to exclude from variables matching the specified variable stem. Defaults to NULL, which retains all values.

Value

A tibble showing the relative frequencies and/or percentages of multiple response variables sharing a common variable stem.

Author

Ama Nyame-Mensah

Examples

select_tbl(data = tas,
           var_stem = "involved_",
           na_removal = "pairwise")
#> # A tibble: 12 × 4
#>    variable                  values count percent
#>    <chr>                      <dbl> <int>   <dbl>
#>  1 involved_arts                  0  2127  0.842 
#>  2 involved_arts                  1   399  0.158 
#>  3 involved_sports                0  2114  0.837 
#>  4 involved_sports                1   412  0.163 
#>  5 involved_schoolClubs           0  2127  0.858 
#>  6 involved_schoolClubs           1   352  0.142 
#>  7 involved_election              0  1028  0.452 
#>  8 involved_election              1  1248  0.548 
#>  9 involved_socialActionGrps      0  2419  0.958 
#> 10 involved_socialActionGrps      1   107  0.0424
#> 11 involved_volunteer             0  1732  0.686 
#> 12 involved_volunteer             1   794  0.314 

select_tbl(data = depressive,
           var_stem = "dep",
           na_removal = "listwise",
           pivot = "wider",
           only = "percent")
#> # A tibble: 8 × 4
#>   variable percent_value_1 percent_value_2 percent_value_3
#>   <chr>              <dbl>           <dbl>           <dbl>
#> 1 dep_1             0.0678           0.429          0.503 
#> 2 dep_2             0.0896           0.464          0.446 
#> 3 dep_3             0.723            0.244          0.0330
#> 4 dep_4             0.374            0.520          0.106 
#> 5 dep_5             0.121            0.346          0.533 
#> 6 dep_6             0.241            0.535          0.224 
#> 7 dep_7             0.640            0.305          0.0554
#> 8 dep_8             0.197            0.488          0.315 

var_label_example <-
  c("dep_1" = "how often child feels sad and blue",
    "dep_2" = "how often child feels nervous, tense, or on edge",
    "dep_3" = "how often child feels happy",
    "dep_4" = "how often child feels bored",
    "dep_5" = "how often child feels lonely",
    "dep_6" = "how often child feels tired or worn out",
    "dep_7" = "how often child feels excited about something",
    "dep_8" = "how often child feels too busy to get everything")

select_tbl(data = depressive,
           var_stem = "dep",
           na_removal = "pairwise",
           pivot = "longer",
           var_labels = var_label_example)
#> # A tibble: 24 × 5
#>    variable variable_label                                  values count percent
#>    <chr>    <chr>                                            <int> <int>   <dbl>
#>  1 dep_1    how often child feels sad and blue                   1   120  0.0726
#>  2 dep_1    how often child feels sad and blue                   2   709  0.429 
#>  3 dep_1    how often child feels sad and blue                   3   825  0.499 
#>  4 dep_2    how often child feels nervous, tense, or on ed…      1   151  0.0920
#>  5 dep_2    how often child feels nervous, tense, or on ed…      2   762  0.464 
#>  6 dep_2    how often child feels nervous, tense, or on ed…      3   728  0.444 
#>  7 dep_3    how often child feels happy                          1  1192  0.721 
#>  8 dep_3    how often child feels happy                          2   406  0.246 
#>  9 dep_3    how often child feels happy                          3    55  0.0333
#> 10 dep_4    how often child feels bored                          1   611  0.371 
#> # ℹ 14 more rows

select_tbl(data = depressive,
           var_stem = "dep",
           na_removal = "pairwise",
           pivot = "wider",
           only = "count",
           var_labels = var_label_example)
#> # A tibble: 8 × 5
#>   variable variable_label              count_value_1 count_value_2 count_value_3
#>   <chr>    <chr>                               <int>         <int>         <int>
#> 1 dep_1    how often child feels sad …           120           709           825
#> 2 dep_2    how often child feels nerv…           151           762           728
#> 3 dep_3    how often child feels happy          1192           406            55
#> 4 dep_4    how often child feels bored           611           856           181
#> 5 dep_5    how often child feels lone…           206           574           871
#> 6 dep_6    how often child feels tire…           399           879           371
#> 7 dep_7    how often child feels exci…          1046           507            95
#> 8 dep_8    how often child feels too …           323           801           519