Skip to contents

Given a rack shelf, or in other words, row of a model rack, calculate boxed index summary. Result is returned in a form of a dataframe.

Usage

shelf_make_indices_boxed(
  shelf,
  data = "data$x",
  cols = everything(),
  boxes,
  na.rm = FALSE
)

Arguments

shelf

A rack shelf, or in other words, a row of a model rack extracted with rack_extract_shelf().

data

One of the following:

  • "data$x" to use model features for indices

  • A name of a column containing dataframe to use its columns for indices

  • a dataframe to take index columns from

cols

A tidyselect expression to subset columns from data. By default is everything(), so all columns are taken.

boxes

A named list describing index boxes. Each element is a character vector listing all levels to pick for a particular box. See examples for details.

na.rm

Drop NA categories before calculating percentages? Default is FALSE.

Examples

# Calculate indices for battery "3" of model columns 
# stats will be calculated for two boxes:
# - "Top 2": when respondents select one of the two positive values:
#     c("Strongly agree", "Somewhat agree")
# - "Top 1": when respondents select only "Strongly agree"
shelf_make_indices_boxed(
  shelf = example_seg_shelf,
  data = "data$x",
  cols = contains("_3_"),
  boxes = list(
    "Top 2" = c("Strongly agree", "Somewhat agree"),
    "Top 1" = c("Strongly agree")
  )
)
#> ! Found 2 columns with missing or invalid labels: "weight" and "segment".
#> ℹ Replacing these labels with {.code NA_character_}
#> ! Found 2 columns with missing or invalid labels: "weight" and "segment".
#> ℹ Replacing these labels with {.code NA_character_}
#> # A tibble: 18 × 7
#>    segment segment_total population_total feature label            `Top 2`$index
#>    <fct>           <dbl>            <dbl> <chr>   <chr>                    <dbl>
#>  1 Yellow           23.7             103. m2_3_17 I think trying …         111. 
#>  2 Red              49.6             103. m2_3_17 I think trying …          97.8
#>  3 Green            29.2             103. m2_3_17 I think trying …          94.4
#>  4 Yellow           23.7             103. m2_3_18 I tend to value…         105. 
#>  5 Red              49.6             103. m2_3_18 I tend to value…         102. 
#>  6 Green            29.2             103. m2_3_18 I tend to value…          93.3
#>  7 Yellow           23.7             103. m2_3_19 I am happy to c…          96.5
#>  8 Red              49.6             103. m2_3_19 I am happy to c…         103. 
#>  9 Green            29.2             103. m2_3_19 I am happy to c…          97.8
#> 10 Yellow           23.7             103. m2_3_21 As I grow older…         134. 
#> 11 Red              49.6             103. m2_3_21 As I grow older…          57.1
#> 12 Green            29.2             103. m2_3_21 As I grow older…         146. 
#> 13 Yellow           23.7             103. m2_3_22 I tend to see t…         140. 
#> 14 Red              49.6             103. m2_3_22 I tend to see t…          85.4
#> 15 Green            29.2             103. m2_3_22 I tend to see t…          92.5
#> 16 Yellow           23.7             103. m2_3_23 I like knowing …         102. 
#> 17 Red              49.6             103. m2_3_23 I like knowing …         104. 
#> 18 Green            29.2             103. m2_3_23 I like knowing …          91.1
#> # ℹ 6 more variables: `Top 2`$pval <dbl>, $segment_perc <dbl>,
#> #   $segment_n <dbl>, $population_perc <dbl>, $population_n <dbl>,
#> #   `Top 1` <tibble[,6]>