The fmt_table1 function calculates descriptive statistics by groups for continuous, categorical, and dichotomous variables. Review the fmt_table1 vignette for detailed examples.

fmt_table1(data, by = NULL, label = NULL, type = NULL,
  statistic = NULL, digits = NULL, id = NULL, missing = c("ifany",
  "always", "no"))

Arguments

data

data frame.

by

a character name of a categorical variable in data, by = "group". Summary statistics will be calculated separately for each level of the by variable. If NULL, summary statistics are calculated using all observations.

label

A list of variable labels, e.g. list(age = "Age, yrs", ptstage = "Path T Stage"). If NULL, the function will take the label attribute (attr(data$age, "label")). If the label doesn't exist, then the label is assigned as the variable name.

type

A list that includes specified summary types. Accepted values are c("continuous", "categorical", "dichotomous"), e.g. type = list(age = "continuous", female = "dichotomous"). If type not specified for a variable, the function will default to an appropriate summary type.

statistic

A list of the type of statistics to return. The list can contain two names lists (continuous and categorical). The value within the list is the types of summary statistics to be returned. For continuous variables the choices are: median, q1 (first quartile), q3 (third quartile), mean, sd (standard deviation), min (minimum), max (maximum). For categorical variables the choices are n (frequency), N (denominator, or cohort size), p (percent). The defaults are continuous = "{median} ({q1}, {q3})" and categorical = "{n} ({p}\%)". The syntax follows from the glue function. Dichotomous variables follow the same format as categorical.

digits

integer indicating the number of decimal places to round continuous summary statistics. sprintf(glue::glue("%.{digits}f"), x)

id

Character vector of an ID or grouping variable. Summary statistics will not be printed for this column. The column may be used in add_comparison to calculate p-values with correlated data. Default is NULL

missing

whether to include NA values in the table. missing controls if the table includes counts of NA values: the allowed values correspond to never ("no"), only if the count is positive ("ifany") and even for zero counts ("always"). Default is "ifany".

Value

Data frame including formatted descriptive statistics.

Examples

fmt_table1(trial, by = "trt")
#> #> Variable Drug Placebo #> N = 107 N = 93 #> Age, yrs 47 (39, 58) 46 (36, 54) #> Unknown 3 5 #> Marker Level, ng/mL 0.61 (0.22, 1.20) 0.72 (0.22, 1.63) #> Unknown 4 4 #> T Stage #> T1 25 (23%) 26 (28%) #> T2 26 (24%) 23 (25%) #> T3 29 (27%) 13 (14%) #> T4 27 (25%) 31 (33%) #> Grade #> I 38 (36%) 29 (31%) #> II 34 (32%) 24 (26%) #> III 35 (33%) 40 (43%) #> Tumor Response 52 (51%) 30 (33%) #> Unknown 6 3
# convert numeric 'am' to factor to display nicely in header mtcars %>% dplyr::mutate(am = factor(am, c(0, 1), c("Automatic", "Manual"))) %>% fmt_table1(by = "am") %>% add_comparison()
#> #> Variable Automatic Manual p-value #> N = 19 N = 13 #> mpg 17.3 (14.9, 19.2) 22.8 (21.0, 30.4) 0.002 #> cyl 0.009 #> 4 3 (16%) 8 (62%) #> 6 4 (21%) 3 (23%) #> 8 12 (63%) 2 (15%) #> disp 276 (196, 360) 120 (79, 160) <0.001 #> hp 175 (116, 192) 109 (66, 113) 0.044 #> drat 3.15 (3.07, 3.70) 4.08 (3.85, 4.22) <0.001 #> wt 3.52 (3.44, 3.84) 2.32 (1.94, 2.78) <0.001 #> qsec 17.82 (17.18, 19.17) 17.02 (16.46, 18.61) 0.3 #> vs 7 (37%) 7 (54%) 0.6 #> gear <0.001 #> 3 15 (79%) 0 (0%) #> 4 4 (21%) 8 (62%) #> 5 0 (0%) 5 (38%) #> carb 0.3 #> 1 3 (16%) 4 (31%) #> 2 6 (32%) 4 (31%) #> 3 3 (16%) 0 (0%) #> 4 7 (37%) 3 (23%) #> 6 0 (0%) 1 (7.7%) #> 8 0 (0%) 1 (7.7%)