---
title: "Table 1"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Table 1}
\usepackage[utf8]{inputenc}
%\VignetteEngine{knitr::rmarkdown}
date: "`r Sys.Date()`"
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Making Table 1
```{r}
library(furniture)
```
This vignette demonstrates the main function of the `furniture` package--`table1()`. This vignette is current as of `furniture` `r packageVersion("furniture")`.
The main parts of the `table1()` are below:
```{r structure, eval=FALSE}
table1(.data, ..., splitby, row_wise, test, type, output, format_number, na.rm)
```
It contains several useful features for summarizing your data:
1. It simply summarizes many variables succinctly providing means/counts and SD's/percentages. By providing variable names to the `medians` option, you can obtain the median and the first quartile/third quantile.
2. The descriptive statistics can be by a grouping factor (i.e., splitby).
3. It uses a similar API to the popular tidyverse groups of packages and can be used in a pipe.
4. It can give bivariate test results for the variable with the grouping variable, which provides the correct test type depending on the variable types.
5. It is flexible as to its output: can be printed in regular console output or it can be printed in latex (either through kable or through a built-in function), markdown, and pandoc (see `knitr::kable`).
6. Numbers can be formatted nicely.
7. Table has multiple formatting options to fit various needs using `output`, `format_output`, `simple` and `condense`.
8. The table can be exported to a CSV with `export = "file_name"`.
To illustrate, we'll walk through the main arguments with an example on some fictitious data.
## Example
```{r data}
set.seed(84332)
## Create Fictitious Data containing several types of variables
df <- data.frame(a = sample(1:10000, 10000, replace = TRUE),
b = runif(10000) + rnorm(10000),
c = factor(sample(c(1,2,3,4,NA), 10000, replace=TRUE)),
d = factor(sample(c(0,1,NA), 10000, replace=TRUE)),
e = trunc(rnorm(10000, 20, 5)),
f = factor(sample(c(0,1,NA), 10000, replace=TRUE)))
```
We will use `df` to show these main features of `table1`.
### The ...
For `table1`, the ellipses (the `...`), are the variables to be summarized that are found in your data. Here, we have `a` through `e` in `df`.
```{r simple}
table1(df,
a, b, c, d, e)
```
### Splitby
To get means/count and SD's/percentages by a stratifying variable, simply use the `splitby` argument. The splitby can be a quoted variable (e.g., `"df"`) or can be a one-sided formula as shown below (e.g., `~d`).
```{r splitby}
table1(df,
a, b, c,
splitby = ~d)
```
### Row Wise
You can get percentages by rows instead of by columns (i.e., groups) by using the `row_wise = TRUE` option.
```{r rowwise}
table1(df,
a, b, c,
splitby = ~d,
row_wise = TRUE)
```
### Test
It is easy to test for bivariate relationships, as in common in many Table 1's, using `test = TRUE`.
```{r test}
table1(df,
a, b, c,
splitby = ~d,
test = TRUE)
```
By default, only the p-values are shown but other options exist such as stars or including the test statistics with the p-values using the `format_output` argument.
## Simple and Condensed
The table can be simplified by just producing percentages for categorical variables. Further, it can be condensed by providing only a reference group's percentages for binary variables and the means and SD's are provided on the same line as the variable name.
```{r s_c}
table1(df,
f, a, b, c,
splitby = ~d,
test = TRUE,
type = c("simple", "condensed"))
```
## Medians
If the medians and the interquartile range is desired instead of means and SD's, simply use the `second` argument:
```{r meds}
table1(df,
f, a, b, c,
splitby = ~d,
test = TRUE,
type = c("simple", "condensed"),
second = c("a", "b"))
```
### Output Type
Several output types exist for the table (all of the `knitr::kable` options) including `html` as shown below. Others include:
1. "latex"
2. "markdown"
3. "pandoc"
```{r html}
table1(df,
a, b, c,
splitby = ~d,
test = TRUE,
output = "html")
```
### Format Number
For some papers you may want to format the numbers by inserting a comma in as a placeholder in big numbers (e.g., 30,000 vs. 30000). You can do this by using `format_number = TRUE`.
```{r formatnumber}
table1(df,
a, b, c,
splitby = ~d,
test = TRUE,
format_number = TRUE)
```
### `na.rm`
In order to explore the missingness in the factor variables, using `na.rm = FALSE` does the counts and percentages of the missing values as well.
```{r nakeep}
table1(df,
a, b, c,
splitby = ~d,
test = TRUE,
na.rm = FALSE)
```
Here we do not have any missingness but it shows up as zeros to show that there are none there.
### Piping
Finally, and very importantly, to make it easier to implement in the tidyverse of packages, a piping option is available. This option can use a `grouped_df` object output by `dplyr::group_by()` and use the groups indicated there as shown below.
```{r tidyverse, fig.width=5, message=FALSE, warning=FALSE}
library(dplyr)
df %>%
filter(f == 1) %>%
group_by(d) %>%
table1(a, b, c,
test = TRUE,
type = c("simple", "condensed"))
```
This includes the ability to use multiple grouping variables. The first value is the first grouping variable, then an underscore, followed by the value of the second grouping variable.
```{r tidyverse2, fig.width=5, message=FALSE, warning=FALSE}
df %>%
group_by(d, f) %>%
table1(a, b, c,
test = TRUE,
type = c("simple", "condensed"))
```
### Variable Names
You can also adjust the variable names from within the function as so:
```{r}
table1(df,
"Avar" = a, "Bvar" = b, "Cvar" = c,
splitby = ~d,
test = TRUE)
```
This is particularly useful when you adjust a variable within the function:
```{r, warning=FALSE, message=FALSE}
df %>%
group_by(d) %>%
table1("A" = factor(ifelse(a > 500, 1, 0)), b, c,
test = TRUE)
```
Here we changed `a` to a factor within the function. In order for the name to look better, we can assign a new name, otherwise it would be named something like `factor.ifelse.a...`.
## Final Note
As a final note, the `"table1"` object can be coerced to a `data.frame` very easily:
```{r dataframe}
tab1 <- table1(df,
a, b, c,
splitby = ~d,
test = TRUE)
as.data.frame(tab1)
```
## Conclusions
`table1` can be a valuable addition to the tools that are being utilized to analyze descriptive statistics. Enjoy this valuable piece of furniture!