create_tidy_table_one: Tidy table one

View source: R/create-tidy-table-one.R

create_tidy_table_oneR Documentation

Tidy table one

Description

Creates a tidy data frame of the results that can go into a "Table 1" of summary descriptive statistics of a study sample. Inspiration for this is owed to the tableone package by Kazuki Yoshida.

Usage

create_tidy_table_one(
  data,
  strata = NULL,
  vars,
  na_level = "(Missing)",
  b_replicates = 2000,
  ...
)

Arguments

data

A data frame or tibble containing the variables to be summarized.

strata

Character vector of the stratifying (grouping) variable.

vars

Character vector of the variable names to be summarized. If empty, then all variables in the given data frame are used.

na_level

Character string of the text to replace NA in the strata variable, if any exist.

b_replicates

an integer specifying the number of replicates used in the Monte Carlo test for Fisher's Exact test and Chi-square test.

...

Additional arguments. Not used.

Value

A tibble with the following results

strata

Level of the stratifying variable

var

Variable/column name

n

Number of records

n_distinct

Numer of distinct values

complete

Number of non-missing observations

missing

Number of missing observations

mean

Mean

sd

Standard deviation

p0

Minimum

p25

25th percentile

p50

Median

p75

75th percentile

p100

Maximum

cv

Coefficient of variation

shapiro_test

Shapiro-Wilkes test: p-value

ks_test

Kolmogorov-Smirnov test: p-value

ad_test

Anderson-Darling test for normality: p-value

level

Level of the variable

n_level

Total number in the variable's group

n_strata

Total number in the variable group and strata

chisq_test

Chi square test: p-value, with continuity correction

chisq_test_no_correction

Chi square test: p-value, without continuity correction

chisq_test_simulated

Chi square test: p-value: simulated p-value

fisher_test

Fisher's exact test: p-value

fisher_test_simulated

Fisher's exact test: simulated p-value

check_categorical_test

Is Chi square OK? Consider Fisher

oneway_test_unequal_var

Oneway anova test: p-value, equivalent to t-test when only 2 groups, unequal variances

oneway_test_equal_var

Oneway anova test: p-value, equivalent to t-test when only 2 groups, equal variances

kruskal_test

Kruskal-Wallis Rank Sum Test: p-value, equivalent to Mann-Whitney U test when only 2 groups

bartlett_test

Bartlett's test for homogeneity of variances: p-value

levene_test

Levene's test for homogeneity of variances: p-value

smd

Standarized mean difference for all pairwise comparisons

Examples

library(dplyr)

tab1 <- create_tidy_table_one(data = pbc_mayo,
                              strata = "trt",
                              vars = c("time",
                                       "status",
                                       "trt",
                                       "age",
                                       "sex",
                                       "ascites",
                                       "hepato",
                                       "spiders",
                                       "edema",
                                       "bili",
                                       "chol",
                                       "albumin",
                                       "copper",
                                       "alk_phos",
                                       "ast",
                                       "trig",
                                       "platelet",
                                       "protime",
                                       "stage"))

dplyr::glimpse(tab1)


library(ggplot2)  # diamonds data set

#### With strata --------------------------------

# Continuous and categoical
(t1 <- create_tidy_table_one(data = diamonds,
                             strata = "cut",
                             vars = c("carat",
                                      # Don't have to include the strata variable
                                      # "cut",
                                      "color",
                                      "clarity",
                                      "depth",
                                      "table",
                                      "price"))
)

dplyr::glimpse(t1)

t1 |>
  adorn_tidytableone()


# Continuous only
(t2 <- create_tidy_table_one(data = diamonds,
                             strata = "cut",
                             vars = c("carat"))
)

t2 |>
  adorn_tidytableone()



# Categorical only
(t3 <- create_tidy_table_one(data = diamonds,
                             strata = "cut",
                             vars = c("color")))

t3 |>
  adorn_tidytableone()


#### Withou strata --------------------------------

# Continuous and categoical
(t1 <- create_tidy_table_one(data = diamonds,
                             strata = NULL,
                             vars = c("carat",
                                      # Don't have to include the strata variable
                                      # "cut",
                                      "color",
                                      "clarity",
                                      "depth",
                                      "table",
                                      "price"))
)

t1 |>
  adorn_tidytableone()


# Continuous only
(t2 <- create_tidy_table_one(data = diamonds,
                             strata = NULL,
                             vars = c("carat"))
)

t2 |>
  adorn_tidytableone()



# Categorical only
(t3 <- create_tidy_table_one(data = diamonds,
                             strata = NULL,
                             vars = c("color")))

t3 |>
  adorn_tidytableone()

emilelatour/tidytableone documentation built on Jan. 6, 2025, 9:20 a.m.