Produces a formatted table of univariate summary statistics with options allowing for stratification by one or more variables, computing of custom summary/association statistics, custom string templates for results, etc.

univariate_table(
    data,
    strata = NULL,
    associations = NULL,
    numeric_summary = c(Summary = "median (q1, q3)"),
    categorical_summary = c(Summary = "count (percent%)"),
    other_summary = NULL,
    all_summary = NULL,
    evaluate = FALSE,
    add_n = FALSE,
    order = NULL,
    labels = NULL,
    levels = NULL,
    format = c("html", "latex", "markdown", "pandoc", "none"),
    variableName = "Variable",
    levelName = "Level",
    sep = "_",
    fill_blanks = "",
    caption = NULL,
    ...
)

Arguments

data

A data.frame.

strata

An additive formula specifying stratification columns. Columns on the left side go down the rows, and columns on the right side go across the columns. Defaults to NULL.

associations

A named list of functions to evaluate with column strata and each variable. Defaults to NULL. See univariate_associations.

numeric_summary

A named vector containing string templates of how results for numeric data should be presented. See details for what is available by default. Defaults to c(Summary = "median (q1, q3)").

categorical_summary

A named vector containing string templates of how results for categorical data should be presented. See details for what is available by default. Defaults to c(Summary = "count (percent%)").

other_summary

A named character vector containing string templates of how results for non-numeric and non-categorical data should be presented. Defaults to NULL.

all_summary

A named character vector containing string templates of additional results applying to all variables. See details for what is available by default. Defaults to NULL.

evaluate

Should the results of the string templates be evaluated as an R expression after filled with their values? See absorb for details. Defaults to FALSE.

add_n

Should the sample size for each stratfication level be added to the result? Defaults to FALSE.

order

Arguments passed to forcats::fct_relevel for reordering the variables. Defaults to NULL

labels

A named character vector containing the new labels. Defaults to NULL

levels

A named list of named character vectors containing the new levels. Defaults to NULL

format

The format that the result should be rendered in. Must be "html", "latex", "markdown", "pandoc", or "none". Defaults to "html".

variableName

Header for the variable column in the result. Defaults to "Variable".

levelName

Header for the factor level column in the result. Defaults to "Level".

sep

Delimiter to separate summary columns. Defaults to "_".

fill_blanks

String to fill in blank spaces in the result. Defaults to "".

caption

Caption for resulting table passed to knitr::kable. Defaults to NULL.

...

Additional arguments to pass to descriptives.

Value

A table of summary statistics in the specified format. A tibble::tibble is returned if format = "none".

Author

Alex Zajichek

Examples


#Set format
format <- "pandoc"

#Default summary
heart_disease %>%
    univariate_table(
      format = format
    )
#> 
#> 
#> Variable                Level              Summary          
#> ----------------------  -----------------  -----------------
#> Age                                        56 (48, 61)      
#> Sex                     Female             97 (32.01%)      
#>                         Male               206 (67.99%)     
#> ChestPain               Typical angina     23 (7.59%)       
#>                         Atypical angina    50 (16.5%)       
#>                         Non-anginal pain   86 (28.38%)      
#>                         Asymptomatic       144 (47.52%)     
#> BP                                         130 (120, 140)   
#> Cholesterol                                241 (211, 275)   
#> MaximumHR                                  153 (133.5, 166) 
#> ExerciseInducedAngina   No                 204 (67.33%)     
#>                         Yes                99 (32.67%)      
#> HeartDisease            No                 164 (54.13%)     
#>                         Yes                139 (45.87%)     

#Stratified summary
heart_disease %>%
    univariate_table(
        strata = ~Sex,
        add_n = TRUE,
        format = format
    )
#> 
#> 
#> Variable                Level              Female (N=97)    Male (N=206)        
#> ----------------------  -----------------  ---------------  --------------------
#> Age                                        57 (50, 63)      54.5 (47, 59.75)    
#> ChestPain               Typical angina     4 (4.12%)        19 (9.22%)          
#>                         Atypical angina    18 (18.56%)      32 (15.53%)         
#>                         Non-anginal pain   35 (36.08%)      51 (24.76%)         
#>                         Asymptomatic       40 (41.24%)      104 (50.49%)        
#> BP                                         132 (120, 140)   130 (120, 140)      
#> Cholesterol                                254 (215, 302)   235 (208.75, 268.5) 
#> MaximumHR                                  157 (142, 165)   150.5 (132, 167.5)  
#> ExerciseInducedAngina   No                 75 (77.32%)      129 (62.62%)        
#>                         Yes                22 (22.68%)      77 (37.38%)         
#> HeartDisease            No                 72 (74.23%)      92 (44.66%)         
#>                         Yes                25 (25.77%)      114 (55.34%)        

#Row strata with custom summaries with
heart_disease %>%
    univariate_table(
        strata = HeartDisease~1,
        numeric_summary = c(Mean = "mean", Median = "median"),
        categorical_summary = c(`Count (%)` = "count (percent%)"),
        categorical_types = c("factor", "logical"),
        add_n = TRUE,
        format = format
    )
#> 
#> 
#> HeartDisease   Variable                Level              Mean     Median   Count (%)    
#> -------------  ----------------------  -----------------  -------  -------  -------------
#> No (N=164)     Age                                        52.59    52                    
#>                Sex                     Female                               72 (43.9%)   
#>                                        Male                                 92 (56.1%)   
#>                ChestPain               Typical angina                       16 (9.76%)   
#>                                        Atypical angina                      41 (25%)     
#>                                        Non-anginal pain                     68 (41.46%)  
#>                                        Asymptomatic                         39 (23.78%)  
#>                BP                                         129.25   130                   
#>                Cholesterol                                242.64   234.5                 
#>                BloodSugar              FALSE                                141 (85.98%) 
#>                                        TRUE                                 23 (14.02%)  
#>                MaximumHR                                  158.38   161                   
#>                ExerciseInducedAngina   No                                   141 (85.98%) 
#>                                        Yes                                  23 (14.02%)  
#> Yes (N=139)    Age                                        56.63    58                    
#>                Sex                     Female                               25 (17.99%)  
#>                                        Male                                 114 (82.01%) 
#>                ChestPain               Typical angina                       7 (5.04%)    
#>                                        Atypical angina                      9 (6.47%)    
#>                                        Non-anginal pain                     18 (12.95%)  
#>                                        Asymptomatic                         105 (75.54%) 
#>                BP                                         134.57   130                   
#>                Cholesterol                                251.47   249                   
#>                BloodSugar              FALSE                                117 (84.17%) 
#>                                        TRUE                                 22 (15.83%)  
#>                MaximumHR                                  139.26   142                   
#>                ExerciseInducedAngina   No                                   63 (45.32%)  
#>                                        Yes                                  76 (54.68%)