Title: | Publication Quality Descriptive Statistics Tables |
---|---|
Description: | Provides functions to create descriptive statistics tables for continuous and categorical variables. By default, summary statistics such as mean, standard deviation, quantiles, minimum and maximum for continuous variables and relative and absolute frequencies for categorical variables are calculated. 'DescrTab2' features a sophisticated algorithm to choose appropriate test statistics for your data and provides p-values. On top of this, confidence intervals for group differences of appropriated summary measures are automatically produces for two-group comparison. Tables generated by 'DescrTab2' can be integrated in a variety of document formats, including .html, .tex and .docx documents. 'DescrTab2' also allows printing tables to console and saving table objects for later use. |
Authors: | Jan Meis [aut, cre] , Lukas Baumann [aut] , Maximilian Pilz [aut] , Lukas Sauer [aut] |
Maintainer: | Jan Meis <[email protected]> |
License: | GPL (>=3) |
Version: | 2.1.26 |
Built: | 2024-11-11 05:17:09 UTC |
Source: | https://github.com/imbi-heidelberg/descrtab2 |
Load LaTeX packages
.onLoad(libname = find.package("kableExtra"), pkgname = "kableExtra")
.onLoad(libname = find.package("kableExtra"), pkgname = "kableExtra")
libname |
library name |
pkgname |
package name |
Thanks to Hao Zhu and his package kableExtra.
Hao Zhu
From https://stackoverflow.com/a/47677916
can.be.numeric(x)
can.be.numeric(x)
x |
a vector |
This is useful if you work with lots of separate SAS datasets spread out in the same folder.
codegen_load_all_sas_data(dir, format = NULL)
codegen_load_all_sas_data(dir, format = NULL)
dir |
path to dataset folder |
format |
path to format file |
NULL. Relevant code is printed to the console.
codegen_load_all_sas_data(system.file("examples", package = "DescrTab2"))
codegen_load_all_sas_data(system.file("examples", package = "DescrTab2"))
Function to create (a part of a) nicely formatted table
create_character_subtable( DescrVarObj, var_name, format_options, format_summary_stats, format_p, reshape_rows )
create_character_subtable( DescrVarObj, var_name, format_options, format_summary_stats, format_p, reshape_rows )
DescrVarObj |
Variable object to be formatted |
var_name |
(character) Name of the variable |
format_options |
named list of options for formatting |
format_summary_stats |
named list of summary statistcs |
format_p |
formatting function for p-values |
reshape_rows |
named list of row reshaping functions |
Function to create (a part of a) nicely formatted table
create_numeric_subtable( DescrVarObj, var_name, format_options, format_summary_stats, format_p, reshape_rows )
create_numeric_subtable( DescrVarObj, var_name, format_options, format_summary_stats, format_p, reshape_rows )
DescrVarObj |
Variable object to be formatted |
var_name |
(character) Name of the variable |
format_options |
named list of options for formatting |
format_summary_stats |
named list of summary statistcs |
format_p |
formatting function for p-values |
reshape_rows |
named list of row reshaping functions |
Generate a list of descriptive statistics. By default, the function calculates summary statistics such as mean, standard deviation, quantiles, minimum and maximum for continuous variables and relative and absolute frequencies for categorical variables. Also calculates p-values for an appropriately chosen statistical test. For two-group comparisons, confidence intervals for appropriate summary measures of group differences are calculated aswell. In particular, Wald confidence intervals from prop.test are used for categorical variables with 2 levels, confidence intervals from t.test are used for continuous variables and confidence intervals for the Hodges-Lehman estimator [1] from wilcox.test are used for ordinal variables.
descr( dat, group = NULL, group_labels = list(), var_labels = list(), var_options = list(), summary_stats_cont = list(N = DescrTab2:::.N, Nmiss = DescrTab2:::.Nmiss, mean = DescrTab2:::.mean, sd = DescrTab2:::.sd, median = DescrTab2:::.median, Q1 = DescrTab2:::.Q1, Q3 = DescrTab2:::.Q3, min = DescrTab2:::.min, max = DescrTab2:::.max), summary_stats_numeric_ord = list(N = DescrTab2:::.factorN, Nmiss = DescrTab2:::.factorNmiss, mean = DescrTab2:::.factormean, sd = DescrTab2:::.factorsd, median = DescrTab2:::.factormedian, Q1 = DescrTab2:::.factorQ1, Q3 = DescrTab2:::.factorQ3, min = DescrTab2:::.factormin, max = DescrTab2:::.factormax), summary_stats_cat = list(), format_summary_stats = list(N = function(x) { format(x, digits = 3, scientific = 4) }, mean = function(x) { format(x, digits = 3, scientific = 4) }, sd = function(x) { format(x, digits = 3, scientific = 4) }, median = function(x) { format(x, digits = 3, scientific = 4) }, Q1 = function(x) { format(x, digits = 3, scientific = 4) }, Q3 = function(x) { format(x, digits = 3, scientific = 4) }, min = function(x) { format(x, digits = 3, scientific = 4) }, max = function(x) { format(x, digits = 3, scientific = 4) }, CI = function(x) { format(x, digits = 3, scientific = 4) }), format_p = scales::pvalue_format(), format_options = list(print_Total = NULL, print_p = TRUE, print_CI = FALSE, combine_mean_sd = FALSE, combine_median_Q1_Q3 = FALSE, omit_factor_level = "none", omit_Nmiss_if_0 = TRUE, omit_missings_in_group = TRUE, percent_accuracy = 0.1, percent_suffix = "%", row_percent = FALSE, Nmiss_row_percent = FALSE, absolute_relative_frequency_mode = c("both", "only_absolute", "only_relative"), omit_missings_in_categorical_var = FALSE, categorical_missing_percent_mode = c("no_missing_percent", "missing_as_regular_category", "missing_as_separate_category"), caption = NULL, replace_empty_string_with_NA = TRUE, categories_first_summary_stats_second = FALSE, max_first_col_width = 7.5), test_options = list(paired = FALSE, nonparametric = FALSE, exact = FALSE, var_equal = FALSE, indices = c(), guess_id = FALSE, include_group_missings_in_test = FALSE, include_categorical_missings_in_test = FALSE, test_override = NULL, additional_test_args = list(), boschloo_max_n = 200), reshape_rows = list(`Q1 - Q3` = list(args = c("Q1", "Q3"), fun = function(Q1, Q3) { paste0(Q1, " -- ", Q3) }), `min - max` = list(args = c("min", "max"), fun = function(min, max) { paste0(min, " -- ", max) })), ... )
descr( dat, group = NULL, group_labels = list(), var_labels = list(), var_options = list(), summary_stats_cont = list(N = DescrTab2:::.N, Nmiss = DescrTab2:::.Nmiss, mean = DescrTab2:::.mean, sd = DescrTab2:::.sd, median = DescrTab2:::.median, Q1 = DescrTab2:::.Q1, Q3 = DescrTab2:::.Q3, min = DescrTab2:::.min, max = DescrTab2:::.max), summary_stats_numeric_ord = list(N = DescrTab2:::.factorN, Nmiss = DescrTab2:::.factorNmiss, mean = DescrTab2:::.factormean, sd = DescrTab2:::.factorsd, median = DescrTab2:::.factormedian, Q1 = DescrTab2:::.factorQ1, Q3 = DescrTab2:::.factorQ3, min = DescrTab2:::.factormin, max = DescrTab2:::.factormax), summary_stats_cat = list(), format_summary_stats = list(N = function(x) { format(x, digits = 3, scientific = 4) }, mean = function(x) { format(x, digits = 3, scientific = 4) }, sd = function(x) { format(x, digits = 3, scientific = 4) }, median = function(x) { format(x, digits = 3, scientific = 4) }, Q1 = function(x) { format(x, digits = 3, scientific = 4) }, Q3 = function(x) { format(x, digits = 3, scientific = 4) }, min = function(x) { format(x, digits = 3, scientific = 4) }, max = function(x) { format(x, digits = 3, scientific = 4) }, CI = function(x) { format(x, digits = 3, scientific = 4) }), format_p = scales::pvalue_format(), format_options = list(print_Total = NULL, print_p = TRUE, print_CI = FALSE, combine_mean_sd = FALSE, combine_median_Q1_Q3 = FALSE, omit_factor_level = "none", omit_Nmiss_if_0 = TRUE, omit_missings_in_group = TRUE, percent_accuracy = 0.1, percent_suffix = "%", row_percent = FALSE, Nmiss_row_percent = FALSE, absolute_relative_frequency_mode = c("both", "only_absolute", "only_relative"), omit_missings_in_categorical_var = FALSE, categorical_missing_percent_mode = c("no_missing_percent", "missing_as_regular_category", "missing_as_separate_category"), caption = NULL, replace_empty_string_with_NA = TRUE, categories_first_summary_stats_second = FALSE, max_first_col_width = 7.5), test_options = list(paired = FALSE, nonparametric = FALSE, exact = FALSE, var_equal = FALSE, indices = c(), guess_id = FALSE, include_group_missings_in_test = FALSE, include_categorical_missings_in_test = FALSE, test_override = NULL, additional_test_args = list(), boschloo_max_n = 200), reshape_rows = list(`Q1 - Q3` = list(args = c("Q1", "Q3"), fun = function(Q1, Q3) { paste0(Q1, " -- ", Q3) }), `min - max` = list(args = c("min", "max"), fun = function(min, max) { paste0(min, " -- ", max) })), ... )
dat |
Data frame or tibble. The data set to be analyzed. Can contain continuous or factor (also ordered) variables. |
group |
name (as character) of the group variable in dat. |
group_labels |
named list of labels for the levels of the group variable in dat. |
var_labels |
named list of variable labels. |
var_options |
named list of lists. For each variable, you can have special options that apply only to that variable. These options are specified in this argument. See the details and examples for more explanation. |
summary_stats_cont |
named list of summary statistic functions to be used for numeric variables. |
summary_stats_numeric_ord |
named list of summary statistic function to be used for ordered factor variables which can be converted to numeric. |
summary_stats_cat |
named list of summary statistic function to be used for categorical variables. |
format_summary_stats |
named list of formatting functions for summary statistics. |
format_p |
formatting function for p-values. |
format_options |
named list of formatting options. |
test_options |
named list of test options. |
reshape_rows |
named list of lists. Describes how to combine different summary statistics into the same row. |
... |
further argument to be passed along |
Returns a A DescrList
object, which is a named list of descriptive statistics
which can be passed along to the print function to create
pretty summary tables.
group_labels
and var_labels
need to be named lists of character elements. The names of the list elements have to match the variable
names in your dataset. The values of the list elements are the labels that will be assigned to these variables when printing.
summary_stats_cont
and summary_stats_cat
are both named lists of functions. The names of the list elements are
what will be displayed in the leftmost column of the descriptive table. These functions should take a vector and return
a value.
Each summary statistic has to have an associated formatting function in the format_summary_stats
list.
The functions in format_summary_stats
take a numeric value and convert it to a character string, e.g. 0.2531235 -> "0.2".
The format_p
function converts p-values to character strings, e.g. 0.05 -> "0.05" or 0.000001 -> "<0.001".
Further formatting options can be specified in the format_options
list. It contains the following members:
print_Total
(logical) controls whether to print the "Total" column. If print_Total = NULL, print_Total will be set
to TRUE if test_options$paired == FALSE, else it will be set to FALSE.
print_p
(logical) controls whether to print the p-value column.
print_CI
(logical) controls whether to print the confidence intervals for group-differences.
combine_mean_sd
(logical) controls whether to combine the mean and sd row into one mean ± sd row. This is a
shortcut argument for the specification of an appropriate entry in the reshape_rows
argument.
combine_median_Q1_Q3
(logical) controls whether to combine the median, Q1 and Q3 row into one median (Q1, Q3) row. This is a
shortcut argument for the specification of an appropriate entry in the reshape_rows
argument.
omit_Nmiss_if_0
(logical) controls whether to omit the Nmiss row in continuous variables there are no missings in the variable.
omit_missings_in_group
(logical) controls whether to omit all observations where the group variable is missing.
percent_accuracy
(numeric) A number to round to. Use (e.g.) 0.01 to show 2 decimal places of precision. If NULL, the default, uses a heuristic that
should ensure breaks have the minimum number of digits needed to show the difference between adjacent values. See documentation of scales::label_percent
percent_suffix
(character) the symbol to be used where "%" is appropriate, sensible choices are usually "%" (default) or "" (i.e., empty string)
row_percent
(logical) controls wheter percentages of regular categorical variables should be calculated column-wise (default) or row-wise
Nmiss_row_percent
(logical) controls whether percentages of the "Nmiss"-statistic (number of missing values) should be calculated column-wise (default) or row-wise
absolute_relative_frequency_mode
(character) controls how to display frequencies.
It may be set to one of the following options:
"both"
will display absolute and relative frequencies.
"only_absolute"
will only display absolute frequencies.
"only_relative"
will only display relative frequencies.
omit_missings_in_categorical_var
(logical) controls whether to omit missing values in categorical variables completely.
categorical_missing_percent_mode
(character) controls how to display percentages in categorical variables with a (Missing) category.
It may be set to one of the following options:
"no_missing_percent"
omits a percentage in the missing category entirely.
"missing_as_regular_category"
treats (Missing) as a regular category for %-calculation
This means that if You have three categories: "A" with 10 counts, "B" with 10 counts and "(Missing)" with 10 counts,
they will become "A": 10 (33%), "B": 10 (33%), "(Missing)": 10 (33% purposes.)
"missing_as_separat_category"
calculates (Missing) percentages with respect to
all observations (i.e. #(Missing) / N), but calculates all other catetgory percentages with respect to the non-missing
observations (e.g. #A / N_nonmissing). This means that if You have three categories: "A" with 10 counts, "B" with 10 counts
and "(Missing)" with 10 counts, they will become "A": 10 (50%), "B": 10 (50%), "(Missing)": 10 (33%)
"caption"
adds a table caption to the LaTeX, Word or PDf document
replace_empty_string_with_NA
(logical) controls whether empty strings ("") should be replaced
with missing value (NA_character_
).
categories_first_summary_stats_second
(logical) controls whether the categories should be printed first in the summary statistics table.
max_first_col_width
(numeric) controls the maximum width of the first column in LaTeX tables.
test_options
is a named list with test options. It's members paired
, nonparametric
, and
exact
(logicals) control which test in the corresponding situation. For details, check out the vignette:
https://imbi-heidelberg.github.io/DescrTab2/articles/b_test_choice_tree_pdf.pdf. The test_options = list(test_override="<some test name>")
option can be specified to force usage of a
specific test. This will produce errors if the data does not allow calculation of that specific test, so be wary.
Use print_test_names()
to see a list of all available test names. If paired = TRUE
is specified, you need to supply an index variable
indices
that specifies which datapoints in your dataset are paired. indices
may either be a length one character vector that describes
the name of the index variable in your dataset, or a vector containing the respective indices.
If you have guess_id
set to TRUE
(the default), DescrTab2
will try to guess
the ID variable from your dataset and report a warning if it succeedes.
See https://imbi-heidelberg.github.io/DescrTab2/articles/a_usage_guide.html#Paired-observations-1
for a bit more explanation. The optional list additional_test_args
can be used to pass arguments along to test functions,
e.g. additional_test_args=list(correct=TRUE)
will request continuity correction if available.
The var_options
list can be used to conduct customizations that should only apply to a single variable and leave
the rest of the table unchanged. var_options
is a list of named lists. This means that each member of var_options
is itself a list again.
The names of the list elements of var_options
determine the variables to which the options will apply.
Let's say you have an age
variable in your dataset. To change 'descr' options only for age
, you will need to pass
a list of the form var_options = list(age = list(<Your options here>))
.
You can replace <Your options here>
with the following options:
label
a character string containing the label for the variable
summary_stats
a list of summary statistics. See section "Custom summary statistics"
format_summary_stats
a list of formatting functions for summary statistics. See section "Custom summary statistics"
format_p
a function to format p-values. See section "Custom summary statistics"
format_options
a list of formatting options. See section "Formatting options"
test_options
a list of test options. See section "Test options"
test_override
manually specify the name of the test you want to apply. You can see a list of choices
by typing print_test_names()
. Possible choices are:
"Cochran's Q test"
"McNemar's test"
"Chi-squared goodness-of-fit test"
"Pearson's chi-squared test"
"Exact McNemar's test"
"Boschloo's test"
"Wilcoxon's one-sample signed-rank test"
"Mann-Whitney's U test"
"Kruskal-Wallis's one-way ANOVA"
"Student's paired t-test"
"Mixed model ANOVA"
"Student's one-sample t-test"
"Student's two-sample t-test"
"Welch's two-sample t-test"
"F-test (ANOVA)"
The reshape_rows
argument offers a framework for combining multiple rows of the output table into a single one.
reshape_rows
is a named list of lists. The names of it's member-lists determine the name that will be displayed
as the name of the combined summary stats in the table (e.g. "mean ± sd "). The member lists need to contain two
elements: args
, contains the names of the summary statistics to be combined as characters, and
fun
which contains a function to combine these summary stats. The argument names of this function need to match
the character strings specified in args
. Check out the default options for an exemplary definition.
[1] Hodges, J. L.; Lehmann, E. L. (1963). "Estimation of location based on ranks". Annals of Mathematical Statistics. 34 (2): 598-611. doi:10.1214/aoms/1177704172. JSTOR 2238406. MR 0152070. Zbl 0203.21105. PE euclid.aoms/1177704172
descr(iris) DescrList <- descr(iris) DescrList$variables$results$Sepal.Length$Total$mean print(DescrList) descr(iris, "Species")
descr(iris) DescrList <- descr(iris) DescrList$variables$results$Sepal.Length$Total$mean print(DescrList) descr(iris, "Species")
Publication quality descriptive statistics tables with R
Provides functions to create descriptive statistics tables for continuous and categorical variables. By default, summary statistics such as mean, standard deviation, quantiles, minimum and maximum for continuous variables and relative and absolute frequencies for categorical variables are calculated. 'DescrTab2' features a sophisticated algorithm to choose appropriate test statistics for your data and provides p-values. On top of this, confidence intervals for group differences of appropriated summary measures are automatically produces for two-group comparison. Tables generated by 'DescrTab2' can be integrated in a variety of document formats, including .html, .tex and .docx documents. 'DescrTab2' also allows printing tables to console and saving table objects for later use.
Check out our documentation online: https://imbi-heidelberg.github.io/DescrTab2/
or browse the help files in the Rstudio viewer. You can access the vignettes by typing: browseVignettes("DescrTab2")
The most important function you probably want to check out is called descr
.
Escape LaTeX Symbols
escape_latex_symbols(tibl, numEscapes = 1)
escape_latex_symbols(tibl, numEscapes = 1)
tibl |
A |
numEscapes |
(logical) chooses between "\" and "\\"# |
a tibble
with appropriately escape LaTeX code
Extract the label attribute from data
extract_labels(dat)
extract_labels(dat)
dat |
data in the form of a |
list of labels
a <- c(1, 2) attr(a, "label") <- "b" identical(extract_labels(a), list(a = attr(a, "label")))
a <- c(1, 2) attr(a, "label") <- "b" identical(extract_labels(a), list(a = attr(a, "label")))
The Farrington-Manning test for rate differences can be used to compare the rate difference of successes between two groups to a preset value. It uses an explicit formula for the standard deviation of the test statistic under the null hypothesis [1].
farrington.manning( group1, group2, delta = 0, alternative = "greater", alpha = 0.025 )
farrington.manning( group1, group2, delta = 0, alternative = "greater", alpha = 0.025 )
group1 |
a logical vector of data from group 1, where |
group2 |
a logical vector of data from group 2, where |
delta |
the rate difference under the null hypothesis |
alternative |
character string indicating the alternative to use, either of "two.sided", "less", "greater" |
alpha |
the significance level (acceptable error of the first kind), a two-sided confidence intnerval is returned with confidence level 1 - 2*alpha, such that the lower bound is a valid one sided confidence interval at the confidence level 1 - alpha. |
The Farrington-Maning test for rate differences test the null hypothesis of
for the "two.sided" alternative
(or for the "greater" respectively
for the "less" alternative).
This formulation allows to specify non-inferiority and superiority
test in a consistent manner:
for delta < 0 and alternative == "greater" the null hypothesis
reads and
consequently rejection allows concluding that
i.e. that the rate of success in group one is at least the
success rate in group two plus delta - as delta is negagtive this is equivalent to the success rate of group 1
being at worst |delta| smaller than that of group 2.
for delta >= 0 and alternative == "greater" the null hypothesis
reads and
consequently rejection allows concluding that
i.e. that the rate of success in group one is at least delta greater than the
success rate in group two.
The confidence interval is always computed as two-sided, but with 1-2 confidence level
in case of a one-sided hypthesis. This means that the lower or upper vound are valid one-sided
confidence bounds at level
in this case.
The confidence interval is constructed by inverting the two-sided test directly.
A list of class "htest" containing the following components:
statistic : |
the value of the Z-statistic |
parameter : |
delta, rate difference (group 1 - group 2) under the null hypothesis |
p.value : |
the p-value for the Farrington-Manning test |
null.value : |
rate difference (group 1 - group 2) under the null |
alternative : |
a character string indicating the alternative hypothesis |
method : |
a character string indicating the exact method employed |
data.name : |
a character string giving the names of the data used |
estimate : |
the estimated rate difference (maximum likelihood) |
conf.int : |
a confidence interval for the rate difference |
sample.size : |
the total sample size used for the test |
Kevin Kunzmann
[1] Farrington, Conor P., and Godfrey Manning. "Test statistics and sample size formulae for comparative binomial trials with null hypothesis of non-zero risk difference or non-unity relative risk." Statistics in medicine 9.12 (1990): 1447-1454.
x <- c(rep(TRUE, 20), rep(FALSE, 15)) y <- c(rep(TRUE, 30), rep(FALSE, 25)) farrington.manning(x, y, -.3)
x <- c(rep(TRUE, 20), rep(FALSE, 15)) y <- c(rep(TRUE, 30), rep(FALSE, 25)) farrington.manning(x, y, -.3)
Formatting function for absolute and relative frequencies
format_freqs( numerator, denominator = 1, absolute_relative_frequency_mode = c("both", "only_absolute", "only_relative"), percent_accuracy = NULL, percent_suffix = "%" )
format_freqs( numerator, denominator = 1, absolute_relative_frequency_mode = c("both", "only_absolute", "only_relative"), percent_accuracy = NULL, percent_suffix = "%" )
numerator |
(numeric) numerator for % calculations |
denominator |
(numeric) denominator for % calculations |
absolute_relative_frequency_mode |
one of |
percent_accuracy |
NULL or numeric. Refer to the |
percent_suffix |
usually "%" or "". Refer to the |
string of formatted frequencies
Make an educated guess about the name of the ID variable from a dataset
guess_ID_variable(dat, suppressWarnings = FALSE)
guess_ID_variable(dat, suppressWarnings = FALSE)
dat |
a dataset with names ( |
suppressWarnings |
(logical) suppress warning messages if you know what you are dooing |
if exactly one possible
dat <- data.frame(ID = c(1,2,3,4,5), other = c(1,2,3,4,5)) guess_ID_variable(dat)
dat <- data.frame(ID = c(1,2,3,4,5), other = c(1,2,3,4,5)) guess_ID_variable(dat)
do.call but without an error for unused arguments
ignore_unused_args(what, args)
ignore_unused_args(what, args)
what |
either a function or a non-empty character string naming the function to be called. |
args |
a list of arguments to the function call. The names attribute of args gives the argument names. |
The result of the (evaluated) function call.
# works: DescrTab2:::ignore_unused_args( chisq.test, list(x = factor(c(1, 0, 1, 1, 1, 0)), y = factor(c(0, 1, 1, 0, 1, 0)), abc = 3) ) # would produce error: # do.call(chisq.test, list(x=factor(c(1,0,1,1,1,0)), y=factor(c(0,1,1,0,1,0)), abc=3 ) )
# works: DescrTab2:::ignore_unused_args( chisq.test, list(x = factor(c(1, 0, 1, 1, 1, 0)), y = factor(c(0, 1, 1, 0, 1, 0)), abc = 3) ) # would produce error: # do.call(chisq.test, list(x=factor(c(1,0,1,1,1,0)), y=factor(c(0,1,1,0,1,0)), abc=3 ) )
Wrap cell text in minipage LaTeX environment with stretchy space
in_minipage(text, width, numEscapes = 1, strechSpace = FALSE)
in_minipage(text, width, numEscapes = 1, strechSpace = FALSE)
text |
text to be placed in minipage |
width |
width adjustment |
numEscapes |
(logical) chooses between "\" and "\\"# |
strechSpace |
(logical) will add strethcy space |
appropriate LaTeX code
https://stackoverflow.com/a/50892682
S3 override for knit_print function for DescrList objects.
## S3 method for class 'DescrList' knit_print(x, print_format = options("print_format")[[1]], silent = FALSE, ...)
## S3 method for class 'DescrList' knit_print(x, print_format = options("print_format")[[1]], silent = FALSE, ...)
x |
a |
print_format |
b |
silent |
c |
... |
abc |
outputs formatted table depending on the environment (.RMD) which it is called from
S3 override for knit_print function for DescrPrint objects.
## S3 method for class 'DescrPrint' knit_print(x, print_format = print_format, silent = silent, ...)
## S3 method for class 'DescrPrint' knit_print(x, print_format = print_format, silent = silent, ...)
x |
a |
print_format |
b |
silent |
c |
... |
abc |
outputs formatted table depending on the environment (.RMD) which it is called from
Convenience function to apply descr to a list of datasets and print the results
lapply_descr(list, ...)
lapply_descr(list, ...)
list |
a list of datasets (tibbles or data.frames) |
... |
arguments to be passed to the descr call |
something printable.
l <- list() for (i in 1:2){ l <- append(l, list(iris)) } lapply_descr(l, group="Species")
l <- list() for (i in 1:2){ l <- append(l, list(iris)) } lapply_descr(l, group="Species")
Create a markdown listing from a character dataset
list_freetext_markdown(dat)
list_freetext_markdown(dat)
dat |
a character |
string containing markdown code listing all nonempty free text in the dataset
dat <- data.frame(Freetext = c("Some text", "More text")) list_freetext_markdown(dat) # use inside a .Rmd document like this: # `r list_freetext_markdown(dat)`
dat <- data.frame(Freetext = c("Some text", "More text")) list_freetext_markdown(dat) # use inside a .Rmd document like this: # `r list_freetext_markdown(dat)`
Digits before decimal -1
n_int_digits(x)
n_int_digits(x)
x |
a |
https://stackoverflow.com/questions/47190693/count-the-number-of-integer-digits
a
Useful to extract factor formatting information contained in a proc format SAS statement.
parse_formats( path_to_format_definition, ignore_keywords = c("value"), encoding = "ISO-8859-1" )
parse_formats( path_to_format_definition, ignore_keywords = c("value"), encoding = "ISO-8859-1" )
path_to_format_definition |
(string) Path to the text file to be parsed |
ignore_keywords |
A vector of keywords to be ignored when searching for the name of the variable to be formatted |
encoding |
Encoding for the text file |
A named list with format definitions
tmpfile <- tempfile() write( "proc format; value yn 1=\"yes\" 0=\"no\"; value sex 1=\"female\" 0=\"male\"; run;",tmpfile) parse_formats(tmpfile)
tmpfile <- tempfile() write( "proc format; value yn 1=\"yes\" 0=\"no\"; value sex 1=\"female\" 0=\"male\"; run;",tmpfile) parse_formats(tmpfile)
Prints all possible tests names
print_test_names()
print_test_names()
Returns the names of all possible test names you can specify.
print_test_names()
print_test_names()
This function takes a DescrList object and converts it to either a DescrPrintCharacter or DescrPrintNumeric object, depending on the print_format option. This object is then printed in an appropriate format.
## S3 method for class 'DescrList' print(x, print_format = options("print_format")[[1]], silent = FALSE, ...)
## S3 method for class 'DescrList' print(x, print_format = options("print_format")[[1]], silent = FALSE, ...)
x |
A |
print_format |
Possible values: "console" (default), "tex", "html", "word", "numeric" |
silent |
I TRUE, suppresses output to stdout. |
... |
further arguments to be passed along to print method |
There is no way to convert between DescrPrintCharacter and DescrPrintNumeric objects. The first type is for what you would usually want, the second type is mostly for debugging purposes. A DescrPrintCharacter object can be printed as html, tex code, as a flextable object or simply to the console.
A DescrPrint object which can be printed in various formats.
You can use the print_format
option to control the output type. If you use 'DescrTab2' inside an .Rmd document,
you can set the clobal option option(print_format="tex")
or option(print_format="html")
or
option(print_format="word")
depending on your document type. This way, all your tables will be printed in the
right format by default inside this document.
print(descr(iris), print_format = "console") print(descr(iris), print_format = "tex") print(descr(iris), print_format = "html") print(descr(iris), print_format = "word") print(descr(iris), print_format = "numeric") options(print_format = "tex") descr(iris) options(print_format = "console") descr(iris) DescrPrint <- print(descr(iris)) DescrPrint$variables$results$Sepal.Length$Total$mean print(DescrPrint)
print(descr(iris), print_format = "console") print(descr(iris), print_format = "tex") print(descr(iris), print_format = "html") print(descr(iris), print_format = "word") print(descr(iris), print_format = "numeric") options(print_format = "tex") descr(iris) options(print_format = "console") descr(iris) DescrPrint <- print(descr(iris)) DescrPrint$variables$results$Sepal.Length$Total$mean print(DescrPrint)
This function is specifically tailored to the way the default import script
provided by a Redcap database functions. First, the Hmisc
package is loaded.
The .csv file containing the data is assumed to be located in the current working directory.
Labels are assigned to all variables. Variables which are supposed to be factors are twice,
once as a factor and once in an unformatted way.
read_redcap_formatted(path_to_redcap_script = NULL)
read_redcap_formatted(path_to_redcap_script = NULL)
path_to_redcap_script |
(character) Path to the (automatically generated) redcap script for data import |
This script removes the "unformatted factor" variables and properly assignes labels.
tibble with data
path_to_redcap_script <- system.file("examples", "testredcap.r", package = "DescrTab2") read_redcap_formatted(path_to_redcap_script)
path_to_redcap_script <- system.file("examples", "testredcap.r", package = "DescrTab2") read_redcap_formatted(path_to_redcap_script)
Convencience function to load SAS datasets
read_sas_formatted(path_to_data = NULL, path_to_format = NULL)
read_sas_formatted(path_to_data = NULL, path_to_format = NULL)
path_to_data |
path to .sas7bdat file |
path_to_format |
path to .sas7bcat file |
tibble with data
path_to_data <- system.file("examples", "testsas.sas7bdat", package = "DescrTab2") pat_to_format <- system.file("examples", "formats.sas7bcat", package = "DescrTab2") read_sas_formatted(path_to_data, pat_to_format)
path_to_data <- system.file("examples", "testsas.sas7bdat", package = "DescrTab2") pat_to_format <- system.file("examples", "formats.sas7bcat", package = "DescrTab2") read_sas_formatted(path_to_data, pat_to_format)
Calculates a statistical significance test
sig_test( var, group = NULL, test_options = list(), test = NULL, var_name = NULL )
sig_test( var, group = NULL, test_options = list(), test = NULL, var_name = NULL )
var |
A variable (a vector). |
group |
A variable containing the grouping information. |
test_options |
Named list containing test options. |
test |
Name of a statistical test. |
var_name |
Name of variable to be tested (only used in warning messages). |
A list of test test results.
cont_var <- c(1, 2, 3) sig_test(cont_var)
cont_var <- c(1, 2, 3) sig_test(cont_var)
Format number to a specified number of digits, considering threshold for usage of scientific notation
sigfig( x, digits = 3, scientific_high_threshold = 6, scientific_low_threshold = -6, force_0_behind_0 = FALSE )
sigfig( x, digits = 3, scientific_high_threshold = 6, scientific_low_threshold = -6, force_0_behind_0 = FALSE )
x |
a |
digits |
a |
scientific_high_threshold |
a |
scientific_low_threshold |
a |
force_0_behind_0 |
a |
a
Generator function for nice formatting functions
sigfig_gen( digits = 3, scientific_high_threshold = 6, scientific_low_threshold = -6, force_0_behind_0 = FALSE )
sigfig_gen( digits = 3, scientific_high_threshold = 6, scientific_low_threshold = -6, force_0_behind_0 = FALSE )
digits |
a |
scientific_high_threshold |
a |
scientific_low_threshold |
a |
force_0_behind_0 |
a |
This function seperates a datasets into three parts: "Singular" data, which is the
data from non-repeating instruments. "missings_everywhere", which is data which is missing for each row.
The last parts are all the repeating instruments, which are referred to by their name as recorded in
dat$redcap_repeat_instrument
.
split_redcap_dataset(dat, id_name = "patid")
split_redcap_dataset(dat, id_name = "patid")
dat |
a |
id_name |
(character) the name of the subject ID variable. |
a list of datasets separated into the categories as described
path_to_redcap_script <- system.file("examples", "testredcap.r", package = "DescrTab2") dat <- read_redcap_formatted(path_to_redcap_script) d <- split_redcap_dataset(dat, guess_ID_variable(dat, TRUE))
path_to_redcap_script <- system.file("examples", "testredcap.r", package = "DescrTab2") dat <- read_redcap_formatted(path_to_redcap_script) d <- split_redcap_dataset(dat, guess_ID_variable(dat, TRUE))
Remove the label attribute from data
unlabel(dat)
unlabel(dat)
dat |
data in the form of a |
data with the labels removed
a <- c(1, 2) attr(a, "label") <- "b" identical(unlabel(a), c(1, 2))
a <- c(1, 2) attr(a, "label") <- "b" identical(unlabel(a), c(1, 2))
Function that returns true in CRAN submission
write_in_tmpfile_for_cran()
write_in_tmpfile_for_cran()
TRUE for CRAN submission, FALSE otherwise