pt_demographics.Rd
This function makes a single table from both continuous and categorical data.
pt_demographics(
data,
cols_cont,
cols_cat,
span = NULL,
units = NULL,
table = NULL,
stat_name = "Statistic",
stat_width = 2,
summarize_all = TRUE,
all_name = "All data",
fun = dem_cont_fun,
notes = pt_demographics_notes(),
paneled = TRUE
)
the data frame to summarize; the user should filter or subset so that data contains exactly the records to be summarized; pmtables will not add or remove rows prior to summarizing data
the continuous data columns to summarize; this argument may be specified as a character vector, comma-separated string or quosure
the categorical columns to summarize; this argument may be specified as a character vector, comma-separated string or quosure
variable name for column spanner
optional units for each summarized column; must be a named list
where the names correspond with continuous data columns in data
a named list to use for renaming columns (see details and examples)
name of statistic column
width (in cm) of the statistic column
logical; if TRUE
, summaries across all span
levels will be appended to the right hand side of the table
a character name for the all data summary invoked by
summarize_all
The summary function to use for summarizing the continuous
data; the default is dem_cont_fun()
. The result will be validated with
validate_dem_fun()
.
notes a character vector of notes to place under the table
logical; if TRUE
, the table will be paneled with the
covariate names; otherwise, the covariate names will appear as the left-most
column with non-repeating names cleared and separated with hline
(see
examples).
An object of class pmtable
.
An object with class pmtable; see class-pmtable.
When a continuous data summary function (fun
) is passed, the user should
also pass a set of notes that explain the summary statistics produced
by that function. If no notes are passed, no notes will appear under the
table.
The categorical data is summarized using pt_cat_long()
.
The default summary function for continuous variables is dem_cont_fun()
.
Please review that documentation for details on the default summary for this
table.
If you wish to define your own function, please ensure the output is in the same format. Any number of columns is acceptable.
out <- pt_demographics(
data = pmt_first,
cols_cont = c(Age = "AGE", Weight = "WT"),
cols_cat = c(Sex = "SEXf", Race = "ASIANf"),
units = list(WT = "kg"),
span = c(Study = "STUDYf")
)
out <- pt_demographics(
data = pmt_first,
cols_cont = "AGE,WT",
cols_cat = "SEXf,ASIANf",
paneled = FALSE,
span = "FORMf"
)
tab <- stable(out)
pmtables:::pt_demographics_notes()
#> [1] "Categorical summary is count (percent)"
#> [2] "n: number of records summarized"
#> [3] "SD: standard deviation"
#> [4] "Min: minimum; Max: maximum"
new_fun <- function(value = seq(1,5), name = "", ...) {
value <- value[!is.na(value)]
tibble::tibble(
`mean` = sig(mean(value)),
`median` = sig(median(value)),
`min-max` = paste0(sig(range(value)), collapse = " - ")
)
}
out <- pt_demographics(
data = pmt_first,
cols_cont = "AGE,WT",
cols_cat = "SEXf,ASIANf",
fun = new_fun
)
pmtables:::dem_cont_fun(rnorm(20))
#> # A tibble: 1 × 3
#> `Mean (SD)` `Min / Max` Missing
#> <chr> <chr> <chr>
#> 1 -0.0640 (1.01) -1.53 / 2.62 0
new_fun(rnorm(20))
#> # A tibble: 1 × 3
#> mean median `min-max`
#> <chr> <chr> <chr>
#> 1 -0.0941 -0.177 -2.04 - 1.73