Type: Package
Title: Create Demographic Table
Version: 0.2.2
Description: To create demographic table with simple summary statistics, with optional comparison(s) over one or more groups.
License: GPL-2
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.3.2
Depends: R (≥ 4.5)
Imports: cli, flextable, officer, scales
Suggests: knitr, quarto, rmarkdown
VignetteBuilder: quarto
NeedsCompilation: no
Packaged: 2025-06-13 16:19:12 UTC; tingtingzhan
Author: Tingting Zhan ORCID iD [aut, cre]
Maintainer: Tingting Zhan <tingtingzhan@gmail.com>
Repository: CRAN
Date/Publication: 2025-06-13 16:30:02 UTC

DemographicTable: Create Demographic Table

Description

To create demographic table with simple summary statistics, with optional comparison(s) over one or more groups.

Note

Help files of individual functions are intentionally suppressed in the pdf manual. Users are encouraged to get started with

vignette('intro', package = 'DemographicTable')

Author(s)

Maintainer: Tingting Zhan tingtingzhan@gmail.com (ORCID)


Create Demographic Table

Description

Create a demographic table with simple summary statistics, with optional comparison(s) over one or more groups.

Usage

DemographicTable(
  data,
  data.name = substitute(data),
  groups = NULL,
  exclude = NULL,
  exclude_rx,
  include,
  include_rx,
  robust = TRUE,
  overall = TRUE,
  compare = TRUE,
  pairwise = 3L,
  ...
)

Arguments

data

a data.frame

data.name

character scalar, or the argument call of data. A user-friendly name of the input data.

groups

character scalar or vector, the name(s) of sub-group(s) for which the summary statistics are provided. Default NULL indicating no sub-groups.

exclude

character vector, the name(s) of variable(s) to be excluded. Default NULL indicating no variable are excluded.

exclude_rx

(optional) regex, pattern of the names of the variable(s) to be excluded.

include

character vector, the name(s) of variable(s) to be included. Default names(data) indicating all variables are included.

include_rx

(optional) regex, pattern of the names of the variable(s) to be included.

robust

logical scalar. If TRUE (default), use non-parametric methods for non-normally distributed numeric variables.

overall

logical scalar. If TRUE (default), a column of overall summary statistics will be provided.

compare

logical scalar. If TRUE (default), comparisons between group(s) will be made.

pairwise

integer scalar, minimum number of groups where pairwise comparisons need to be performed. Default 3L.

...

additional parameters, currently not in use

Details

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, p-value of Shapiro-Wilk normality test and ranges. If group is specified, they are compared using two-sample t.test, wilcox.test (Wilcoxon / Mann-Whitney), one-way aov (ANOVA) and/or kruskal.test (Kruskal-Wallis).

logical and factor variables are summarized in counts and percentages. If group is specified, they are compared using prop.test (chi-squared) and/or fisher.test (Fisher's exact).

Value

Function DemographicTable() returns an object of S3 class 'DemographicTable', which is a listof 'sumtab' elements.


Description for DemographicTable

Description

..

Usage

Sprintf.DemographicTable(x)

Arguments

x

a DemographicTable

Value

Function Sprintf.DemographicTable() returns a character scalar.


Convert DemographicTable to flextable

Description

Convert a DemographicTable to a flextable object.

Usage

## S3 method for class 'DemographicTable'
as_flextable(x, ...)

Arguments

x

a DemographicTable object

...

additional parameters, not currently in use

Value

Function as_flextable.DemographicTable() returns a flextable.

Note

End user may use function set_caption to add a caption to the output demographic table.


Concatenate DemographicTable Objects

Description

Concatenate DemographicTable Objects

Usage

## S3 method for class 'DemographicTable'
c(...)

Arguments

...

one or more DemographicTable objects

Value

Function c.DemographicTable() returns a DemographicTable object.


Various warning and/or stop Messages

Description

Various warning and/or stop Messages

Usage

msg_logical()

Examples

msg_logical() |> cat()

p-value from modified Shapiro-Wilk Normality Test

Description

Obtain p-value from shapiro.test, taking into consideration of several exceptions.

Usage

pval_shapiro(x, CLT = FALSE, ...)

Arguments

x

double vector

CLT

logical scalar, whether to allow the use of Central Limit Theorem, default FALSE

...

additional parameters, currently not in use

Details

Function pval_shapiro() provides a pseudo p-value for several exceptions of shapiro.test, serving as a criteria of whether robust statistics/tests need to be used

length(x) < 3L

returns p=0, indicating that robust methods are needed.

length(x) > 5e3L

returns p=1, indicating that no robust method is needed. For such large sample size, robust methods could be too slow.

CLT & length(x) > 30L

returns p=1, indicating that no robust method is needed because of the use of Central Limit Theorem.

all x values are identical (or all.equal, to be implemented in future release)

returns p=0, indicating that robust methods are needed.

Otherwise

use the p-value from shapiro.test.

Value

Function pval_shapiro() returns a double scalar.

Examples

rnorm(5) |> pval_shapiro()
airquality |>
 with(expr = split(Ozone, f = Month)) |>
 sapply(FUN = pval_shapiro)

Summary Statistics

Description

Provide the summary statistics of an R object

Usage

.sumstat(x, fmt, ...)

## Default S3 method:
.sumstat(x, fmt = "%.2f", ...)

## S3 method for class 'factor'
.sumstat(x, ...)

## S3 method for class 'ordered'
.sumstat(x, ...)

## S3 method for class 'character'
.sumstat(x, ...)

## S3 method for class 'logical'
.sumstat(x, ...)

Arguments

x

an R object

fmt

(optional) character scalar, only for function .sumstat.default, see function sprintf

...

additional parameters, currently not in use

Details

Function .sumstat.default() accepts all R objects of typeof double and integer. Statistics of mean, sd and range are reported. If the normality assumption via shapiro.test is not satisfied, then median and mad are also reported.

Value

Function .sumstat() returns a character scalar.

Examples

airquality$Solar.R |> .sumstat()
penguins$sex |> .sumstat()

mirror server hosted at Truenetwork, Russian Federation.