DemographicTable {DemographicTable} | R Documentation |
Create a demographic table with simple summary statistics, with optional comparison(s) over one or more groups.
DemographicTable(
data,
data.name = substitute(data),
groups = NULL,
keep_missing_group = TRUE,
exclude = NULL,
exclude_pattern,
include,
include_pattern,
overall = TRUE,
compare = TRUE,
...
)
data |
|
data.name |
character scalar, or the argument call of |
groups |
character scalar or vector,
the name(s) of sub-group(s) for which the summary statistics are to be provided.
Default |
keep_missing_group |
logical scalar.
If |
exclude |
character vector,
the name(s) of variable(s) to be excluded.
Default |
exclude_pattern |
(optional) character scalar as regular expression, the pattern of the names of the variable(s) to be excluded. |
include |
character vector,
the name(s) of variable(s) to be included.
Default |
include_pattern |
character scalar as regular expression, the pattern of the names of the variable(s) to be included. |
overall |
logical scalar.
If |
compare |
logical scalar.
If |
... |
potential parameters |
A demographic table with simple summary statistics, with optional comparison(s) over one or more groups, is created.
Numeric variables are summarized in means, standard deviations, medians, inter-quartile-ranges (IQR),
skewness, Shapiro-Wilk normality test and ranges.
If group
is specified, they are compared using two-sample t-test,
Wilcoxon / Mann-Whitney test, one-way ANOVA and/or
Kruskal-Wallis test.
logical and factor variables are summarized in counts and percentages.
If group
is specified, they are compared using chi-squared test
and/or Fisher exact test.
DemographicTable returns an object of S3 class 'DemographicTable'
,
which inherits from matrix.
DemographicTable(esoph)
DemographicTable(ToothGrowth, groups = 'supp')
DemographicTable(ToothGrowth, groups = 'supp', compare = FALSE)
DemographicTable(warpbreaks, groups = c('wool', 'tension'))
DemographicTable(mtcars, groups = c('vs', 'am'), include = c('mpg', 'cyl', 'disp'))
# with missing value
DemographicTable(airquality, groups = 'Month', exclude = 'Day')
DemographicTable(MASS::survey, groups = 'Smoke')
DemographicTable(MASS::survey, groups = 'Smoke', keep_missing_group = FALSE)
DemographicTable(MASS::survey, groups = 'Smoke', keep_missing_group = FALSE, useNA = 'always')
# write to Word file
library(flextable)
library(officer)
x = read_docx() |> body_add_flextable(value = as_flextable(DemographicTable(esoph)))
(out = file.path(tempdir(), 'demotable.docx'))
print(x, target = out)
# system(paste('open', out)) # works on Mac & Windows, but requires Microsoft Word
file.remove(out)