# See vignette for more examples:
# utils::vignette('atable_usage', package = 'atable')
# Analyse datasets::ToothGrowth:
# Length of tooth for each dose level and delivery method:
atable::atable(datasets::ToothGrowth,
target_cols = 'len',
group_col = 'supp',
split_cols = 'dose',
format_to = 'Word')
# Print in .docx with e.g. flextable::regulartable and officer::body_add_table
# Analyse datasets::ChickWeight:
# Weight of chickens for each time point and diet:
atable(weight ~ Diet | Time, datasets::ChickWeight, format_to = 'Latex')
# Print as .pdf with e.g. Hmisc::latex
# Analyse atable::test_data:
atable(Numeric + Logical + Factor + Ordered ~ Group | Split1 + Split2,
atable::test_data, format_to = 'HTML')
# Print as .html with e.g. knitr::kable and options(knitr.kable.NA = '')
# Modify atable: calculate median and MAD for numeric variables
new_stats <- function(x, ...){list(Median = median(x, na.rm = TRUE),
MAD = mad(x, na.rm = TRUE))}
atable(atable::test_data,
target_cols = c('Numeric', 'Numeric2'),
statistics.numeric = new_stats,
format_to = 'Console')
# Print in Console with format_to = 'Console'.
# Analyse mtcars and add labels and units of via package Hmisc
mtcars <- within(datasets::mtcars, {gear <- factor(gear)})
# Add labels and units.
attr(mtcars$mpg, 'alias') = 'Consumption [Miles (US)/ gallon]'
Hmisc::label(mtcars$qsec) = 'Quarter Mile Time'
units(mtcars$qsec) = 's'
# apply atable
atable::atable(mpg + hp + gear + qsec ~ cyl | vs,
mtcars,
format_to = 'Console')
# Blocks
# In datasets::mtcars the variables cyl, disp and mpg are related to the engine and am and gear are
# related to the gearbox. So grouping them together is desireable.
atable::atable(datasets::mtcars,
target_cols = c("cyl", "disp", "hp", "am", "gear", "qsec") ,
blocks = list("Engine" = c("cyl", "disp", "hp"),
"Gearbox" = c("am", "gear")),
format_to = "Console")
# Note that Variable qsec is not blocked and thus not indented.
# add_margins
atable::atable(atable::test_data,
target_cols = "Numeric",
group_col = "Group",
split_cols = "Split1",
add_margins = TRUE,
format_to = "Console")
# The column 'Total' contains the results of the ungrouped atable-call:
# The number of observations is the sum of observations of the groups.
# The default of add_margins can be changed via atable_options.
Run the code above in your browser using DataLab