Moonbook significant digit version function to run each variable individually, determining significant digits then join them into a single file at the end

e_table1_summaries(
  dat_tab1 = dat_sub,
  list_var_col_names = NULL,
  list_var_col_labels = NULL,
  list_var_row_names = NULL,
  list_var_row_labels = NULL,
  fn_root = "tab1_vars",
  fn_all = NULL,
  label_width = 40,
  sw_verbose = FALSE,
  moonbook_max_ylev = 2,
  moonbook_digits = c(factor = 1, numeric = 3),
  moonbook_method = c(1, 2, 3)[2]
)

Arguments

dat_tab1

Dataset for table.

list_var_col_names

Column variable name.

list_var_col_labels

Column variable label.

list_var_row_names

Variable names to loop over.

list_var_row_labels

Variable labels to label table.

fn_root

Filename for temporary tables.

fn_all

Filename for output table, defaults to paste0(fn_root, "_", "all", ".csv").

label_width

Width to wrap column and row variable labels, does not affect category labels.

sw_verbose

Print status to console during execution?

moonbook_max_ylev

An integer indicating the maximum number of levels of grouping variable ('y'). If a colummn have unique values less than max.ylev it is treated as a categorical variable. Default value is 2. (?moonBook::mytable_sub)

moonbook_digits

An integer indicating the number of decimal places (round) or significant digits to be used. Default value is 3. (?moonBook::mytable_sub)

moonbook_method

An integer indicating methods for continuous variables. Possible values in methods are: 1 = forces analysis as normal-distributed; 2 = forces analysis as continuous non-normal; 3 = performs a Shapiro-Wilk test to decide between normal or non-normal. (?moonBook::mytable_sub)

Value

NULL, invisibly

Details

Uses moonBook::mytable; default is CatMethod = 2: Numeric summaries are Median and IQR with kruskal.test, Categorical summaries are proportions with chisq.test with continuity correction.

Examples

if (FALSE) {
## For Rmd code chunk attractive html table, code chunk option ```{r, results='asis'}

# specify tows to summarize and columns to summarize by
list_var_col_names <- c("cyl", "am")
list_var_row_names <- c("mpg", "cyl", "disp", "hp", "drat", "wt"
                      , "qsec", "vs", "am", "gear", "carb")

# assigning to new dataset since you may want to filter or subset in some way,
#   for example, only baseline measurements in a longitudinal analysis
dat_tab1 <-
  dat_mtcars_e %>%
  #dplyr::filter(EVENT_ID == "BL") %>%
  dplyr::select(all_of(c(list_var_col_names, list_var_row_names)))


# label variables
list_var_col_labels <- labelled::var_label(dat_tab1[, list_var_col_names]) %>% unlist()
list_var_row_labels <- labelled::var_label(dat_tab1[, list_var_row_names]) %>% unlist()

# This loop creates a table summarized by each variable,
#   but can also specify multiple columns to summarize conditional on multiple columns.
for (i_col in seq_along(list_var_col_names)) {

  # filename root for each separate variable (will write, then read to compile into one)
  fn_root <- paste0("tab1_vars_", list_var_col_names[i_col], "_summary")

  # run function (additional options are available)
  e_table1_summaries(
      dat_tab1           = dat_tab1
    , list_var_col_names = list_var_col_names[i_col]
    , list_var_col_labels = list_var_col_labels[i_col]
    , list_var_row_names = list_var_row_names
    , list_var_row_labels = list_var_row_labels
    , fn_root            = fn_root
    , sw_verbose         = TRUE
    )

  # read final table
  tab1_temp <- utils::read.csv(paste0(fn_root, "_", "all", ".csv"))
  # display in Rmd file
  knitr::kable(tab1_temp)
}

## For totals, helpful when missing data because has sample size for each row.
# filename root for each separate variable (will write, then read to compile into one)
fn_root <- paste0("tab1_vars_", "total", "_summary")

# run function (additional options are available)
e_table1_summaries(
    dat_tab1           = dat_tab1
  , list_var_col_names = NULL
  , list_var_col_labels = "total"
  , list_var_row_names = list_var_row_names
  , list_var_row_labels = list_var_row_labels
  , fn_root            = fn_root
  , sw_verbose         = TRUE
  )

# read final table
tab1_temp <- utils::read.csv(paste0(fn_root, "_", "all", ".csv"))
# display in Rmd file
knitr::kable(tab1_temp)

}