Learn R Programming

furniture (version 1.2.2)

simple_table1: Simple Table 1 for Simple and Stratified Descriptive Statistics

Description

Produces a simple version of Table 1: a descriptive table, stratified by an optional categorical variable, providing means/frequencies and standard deviations/percentages. It is well-formatted for easy transition to academic article or report. Can be used within the piping framework [see library(magrittr)].

Usage

simple_table1(.data, ..., splitby = NULL, row_wise = FALSE, splitby_labels = NULL, test = FALSE, piping = FALSE, rounding = 2, var_names = NULL, format_output = "pvalues", output_type = "text", format_number = FALSE, NAkeep = FALSE, m_label = "Missing", booktabs = TRUE, caption = NULL, align = NULL)

Arguments

.data
the data.frame that is to be summarized
...
variables in the data set that are to be summarized; unquoted names separated by commas (e.g. age, gender, race) or indices. If indices, it needs to be a single vector (e.g. c(1:5, 8, 9:20) instead of 1:5, 8, 9:20). As it is currently, it CANNOT handle both indices and unquoted names simultaneously.
splitby
the categorical variable to stratify by in formula form (e.g., splitby = ~gender); not too surprisingly, it requires that the number of levels be > 0
row_wise
how to calculate percentages for factor variables when splitby != NULL: if FALSE calculates percentages by variable within groups; if TRUE calculates percentages across groups for one level of the factor variable.
splitby_labels
allows for custom labels of the splitby levels; must match the number of levels of the splitby variable
test
logical; if set to TRUE then the appropriate bivariate tests of significance are performed if splitby has more than 1 level
piping
if TRUE then the table is printed and the original data is passed on. It is very useful in piping situations where one wants the table but wants it to be part of a larger pipe.
rounding
the number of digits after the decimal for means and SD's; default is 2
var_names
custom variable names to be printed in the table
format_output
has three options: 1) "full" provides the table with the type of test, test statistic, and the p-value for each variable; 2) "pvalues" provides the table with the p-values; and 3) "stars" provides the table with stars indicating significance
output_type
default is "text"; the other options are all format options in the kable() function in knitr (e.g., latex, html, markdown, pandoc)
format_number
default in FALSE; if TRUE, then the numbers are formatted with commas (e.g., 20,000 instead of 20000)
NAkeep
when sset to TRUE it also shows how many missing values are in the data for each categorical variable being summarized
m_label
when NAkeep = TRUE this provides a label for the missing values in the table
booktabs
when output_type != "text"; option is passed to knitr::kable
caption
when output_type != "text"; option is passed to knitr::kable
align
when output_type != "text"; option is passed to knitr::kable

Value

A table with the number of observations, means/frequencies and standard deviations/percentages is returned. The object is a table1 class object with a print method. Can be printed in LaTex form.

Examples

Run this code
## Ficticious Data ##
library(furniture)
library(tidyverse)

x  <- runif(1000)
y  <- rnorm(1000)
z  <- factor(sample(c(0,1), 1000, replace=TRUE))
a  <- factor(sample(c(1,2), 1000, replace=TRUE))
df <- data.frame(x, y, z, a)

## Simple
simple_table1(df, x, y, z, a)

## Stratified
## both below are the same
simple_table1(df, x, y, z,
              splitby = ~ a)
simple_table1(df, x, y, z,
              splitby = "a")

## Adjust variables within function
simple_table1(df, ifelse(x > 0, 1, 0), z,
              var_names = c("Dich X", "Z"))
         

Run the code above in your browser using DataLab