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"))
data frame.
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.
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.
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.
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.
integer indicating the number of decimal places to round continuous
summary statistics. sprintf(glue::glue("%.{digits}f"), x)
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
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"
.
Data frame including formatted descriptive statistics.
# NOT RUN {
fmt_table1(trial, by = "trt")
# 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()
# }
Run the code above in your browser using DataLab