surv_mean_table: Create crosstab of means and confidence intervals for numeric...

Description Usage Arguments Value Examples

View source: R/mean_tables.R

Description

Takes in a tbl_svy built with the srvyr package, one or more grouping

Usage

1
2
surv_mean_table(s, outcome_vars, grouping_vars = NULL, na.rm = T,
  ci_level = 0.95, displayN = T, prop.to.pct = F, wrap = F)

Arguments

s

A tbl_svy object created with the srvyr package.

outcome_vars

A character vector of outcome variable names. The elements are the existing names of numeric or logical columns to take the means of. If it is a named vector, the names are what they variables should be renamed as.

grouping_vars

Name of factor or character column to group by

na.rm

A logical indicating whether to drop NAs from output table

ci_level

A numeric indicating the confidence level. Default it 0.95

displayN

A logical indicating whether to append the number of observations to each group's name. Ignored if there are zero or multiple grouping variables.

prop.to.pct

A logical indicating whether the outcome_vars are proportions. If TRUE, convert estimates to percents and truncate confidence intervals at 0 and 100

wrap

A logical indicating to return html code that will print the data to the Viewer window in Rstudio for copy and pasting into other programs.

Value

A long formate tibble, or if wrap = T, a kableExtra table in html.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
data(ca_school_tbl_svy)

# one outcome, no groups
surv_mean_table(ca_school_tbl_svy, "qual.teach.prop")

# multiple outcomes, no groups

outcome_vec <- c("eng.lang.learn.prop", "qual.teach.prop", "new.stu.prop")
surv_mean_table(ca_school_tbl_svy, outcome_vec)

# if you plan to rename these outcome variables, create a named vector
  # and let the function rename them to avoid repetition

named_outcome_vec <- c(`Fully qualified teachers` ="qual.teach.prop",
                      `New students`= "new.stu.prop",
                      `English language learners` = "eng.lang.learn.prop")

surv_mean_table(ca_school_tbl_svy, named_outcome_vec)

# prepare for copying or export using wrap (i.e., knitr::knit)
surv_mean_table(ca_school_tbl_svy, named_outcome_vec, wrap = T)

# one grouping varible
surv_mean_table(ca_school_tbl_svy, named_outcome_vec, "poverty")

# one grouping varible, N's not appended to names
surv_mean_table(ca_school_tbl_svy, named_outcome_vec, "poverty", displayN = F)

# multiple grouping variables (note that N's can't be appended)
surv_mean_table(ca_school_tbl_svy, named_outcome_vec, c("poverty", "size"))

# turn all columns into percentages and trim the CIs and 0 and 100
surv_mean_table(ca_school_tbl_svy, named_outcome_vec, c("poverty", "size"),
                 prop.to.pct =T)
rm(ca_school_tbl_svy)

mattysimonson/exploratorium documentation built on Dec. 21, 2021, 3:49 p.m.