atable | R Documentation |
Applies descriptive statistics and hypothesis tests to data, and arranges the results for printing.
atable(x, ...)
## S3 method for class 'data.frame'
atable(
x,
target_cols,
group_col = NULL,
split_cols = NULL,
format_to = atable_options("format_to"),
drop_levels = TRUE,
add_levels_for_NA = FALSE,
blocks = NULL,
add_margins = atable_options("add_margins"),
indent_character = NULL,
indent = atable_options("indent"),
...
)
## S3 method for class 'formula'
atable(formula, data, ...)
x |
An object. If |
... |
Passed from and to other methods. You can use the ellipsis ... to modify atable:
For example the default-statistics for numeric variables are mean and sd. To change these statistics pass
a function to argument See examples below how to modify atable by ... . Actually Here is a list of the statistics and hypothesis tests that can be modified by
|
target_cols |
A character vector containing some column names of Descriptive statistics and hypothesis test are applied to these columns depending on their class.
The descriptive statistics are defined by Hypothesis test are defined by |
group_col |
A character of length 1 containing a column of |
split_cols |
A character vector containing some of |
format_to |
A character vector of length 1. Specifies the format of the output of |
drop_levels |
A logical. If |
add_levels_for_NA |
If |
blocks |
|
add_margins |
A logical with length one, |
indent_character |
A character with length 1 or |
indent |
A logical with length one, |
formula |
A formula of the form |
data |
Passed to |
Results depend on format_to
:
'Raw'
: A list with two elements called 'statistics_result'
and 'tests_result'
, that
contain all results of the descriptive statistics and the hypothesis tests.
This format useful, when extracting a specific result unformulated
(when format_to
is not 'Raw'
all numbers are also returned, but as rounded
characters for printing and squeezed into a data.frame).
'statistics_result'
: contains a data.frame with colnames split_cols, group_col, target_cols
.
split_cols
and group_col
retain their original values (now as factor).
target_cols
contain lists with the results of function statistics
.
As the result of function statistics
is also a list, target_cols
contain lists of lists.
'tests_result'
: has the same structure as 'statistics_result'
, but contains the results
of two_sample_htest
and multi_sample_htest
.
Note that tests_result
only exists if split_cols
is not NULL
.
'Word'
: A data.frame.
Column atable_options('colname_for_group')
contains
all combinations of the levels of split_cols
and
the names of the results of function format_statistics
.
Further columns are the levels of group_col
the names of the results of format_tests
.
The levels of split_cols
and the statistics are arranged vertically.
The hypothesis test are arranged horizontally.
'HTML'
: Same as for format_to = 'Word'
but a different character indents
the first column.
#'
'Console'
: Meant for printing in the R console for interactive analysis.
Same as for format_to = 'Word'
but a different character indents the first column.
'Latex'
: Same as for format_to = 'Word'
but a different character indents
the first column and with translate_to_LaTeX
applied afterwards.
atable(data.frame)
: applies descriptive statistics and hypothesis tests, arranges the results for printing.
atable(formula)
: parses the formula and passes its parts to atable
.
# 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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.