Table 1

2018-04-04

Making Table 1

This vignette demonstrates the main function of the furniture package–table1(). This vignette is current as of furniture v 1.7.2.

The main parts of the table1() are below:

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 ficticious data.

Example

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.

## 
## ────────────────────────
##       Mean/Count (SD/%)
##       n = 10000        
##  a                     
##       4971.4 (2890.5)  
##  b                     
##       0.5 (1.0)        
##  c                     
##     1 1962 (24.5%)     
##     2 2064 (25.8%)     
##     3 1945 (24.3%)     
##     4 2023 (25.3%)     
##  d                     
##     0 3295 (49.8%)     
##     1 3315 (50.2%)     
##  e                     
##       19.4 (5.0)       
## ────────────────────────

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).

## 
## ──────────────────────────────────────
##                    d 
##       0               1              
##       n = 3295        n = 3315       
##  a                                   
##       4949.8 (2868.8) 4969.0 (2884.4)
##  b                                   
##       0.5 (1.0)       0.5 (1.0)      
##  c                                   
##     1 665 (25.3%)     639 (24.1%)    
##     2 652 (24.8%)     677 (25.6%)    
##     3 645 (24.6%)     622 (23.5%)    
##     4 663 (25.3%)     709 (26.8%)    
## ──────────────────────────────────────

Row Wise

You can get percentages by rows instead of by columns (i.e., groups) by using the row_wise = TRUE option.

## 
## ──────────────────────────────────────
##                    d 
##       0               1              
##       n = 3295        n = 3315       
##  a                                   
##       4949.8 (2868.8) 4969.0 (2884.4)
##  b                                   
##       0.5 (1.0)       0.5 (1.0)      
##  c                                   
##     1 665 (33.9%)     639 (32.6%)    
##     2 652 (31.6%)     677 (32.8%)    
##     3 645 (33.2%)     622 (32%)      
##     4 663 (32.8%)     709 (35%)      
## ──────────────────────────────────────

Test

It is easy to test for bivariate relationships, as in common in many Table 1’s, using test = TRUE.

## 
## ──────────────────────────────────────────────
##                        d 
##       0               1               P-Value
##       n = 3295        n = 3315               
##  a                                    0.786  
##       4949.8 (2868.8) 4969.0 (2884.4)        
##  b                                    0.112  
##       0.5 (1.0)       0.5 (1.0)              
##  c                                    0.414  
##     1 665 (25.3%)     639 (24.1%)            
##     2 652 (24.8%)     677 (25.6%)            
##     3 645 (24.6%)     622 (23.5%)            
##     4 663 (25.3%)     709 (26.8%)            
## ──────────────────────────────────────────────

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.

## 
## ──────────────────────────────────────────────
##                        d 
##       0               1               P-Value
##       n = 3295        n = 3315               
##  f: 1 50.4%           50.1%           0.857  
##  a    4949.8 (2868.8) 4969.0 (2884.4) 0.786  
##  b    0.5 (1.0)       0.5 (1.0)       0.112  
##  c                                    0.414  
##     1 25.3%           24.1%                  
##     2 24.8%           25.6%                  
##     3 24.6%           23.5%                  
##     4 25.3%           26.8%                  
## ──────────────────────────────────────────────

Medians

If the medians and the interquartile range is desired instead of means and SD’s, simply use the second argument:

## 
## ──────────────────────────────────────────────
##                        d 
##       0               1               P-Value
##       n = 3295        n = 3315               
##  f: 1 50.4%           50.1%           0.857  
##  a    4958.0 [4902.0] 5031.0 [4937.5] 0.786  
##  b    0.5 [1.4]       0.5 [1.4]       0.112  
##  c                                    0.414  
##     1 25.3%           24.1%                  
##     2 24.8%           25.6%                  
##     3 24.6%           23.5%                  
##     4 25.3%           26.8%                  
## ──────────────────────────────────────────────

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”
0 1 P-Value
n = 3295 n = 3315
a 0.786
4949.8 (2868.8) 4969.0 (2884.4)
b 0.112
0.5 (1.0) 0.5 (1.0)
c 0.414
1 665 (25.3%) 639 (24.1%)
2 652 (24.8%) 677 (25.6%)
3 645 (24.6%) 622 (23.5%)
4 663 (25.3%) 709 (26.8%)

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.

## 
## ──────────────────────────────────────────────────
##                          d 
##       0                 1                 P-Value
##       n = 3295          n = 3315                 
##  a                                        0.786  
##       4,949.8 (2,868.8) 4,969.0 (2,884.4)        
##  b                                        0.112  
##       0.5 (1.0)         0.5 (1.0)                
##  c                                        0.414  
##     1 665 (25.3%)       639 (24.1%)              
##     2 652 (24.8%)       677 (25.6%)              
##     3 645 (24.6%)       622 (23.5%)              
##     4 663 (25.3%)       709 (26.8%)              
## ──────────────────────────────────────────────────

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.

## 
## ───────────────────────────────────────────────
##                         d 
##        0               1               P-Value
##        n = 3295        n = 3315               
##  a                                     0.786  
##        4949.8 (2868.8) 4969.0 (2884.4)        
##  b                                     0.112  
##        0.5 (1.0)       0.5 (1.0)              
##  c                                     0.414  
##     1  665 (20.2%)     639 (19.3%)            
##     2  652 (19.8%)     677 (20.4%)            
##     3  645 (19.6%)     622 (18.8%)            
##     4  663 (20.1%)     709 (21.4%)            
##     NA 670 (20.3%)     668 (20.2%)            
## ───────────────────────────────────────────────

Here we do not have any missingness but it shows up as zeros to show that there are none there.

Piping

Finally, to make it easier to implement in the tidyverse of packages, a piping option is available. This option invisibly returns the data frame that was given to the table 1 function and prints the table in console.

## 
## ──────────────────────────────────────────────
##                        d 
##       0               1               P-Value
##       n = 1112        n = 1107               
##  a    4912.7 (2883.3) 4958.2 (2938.0) 0.713  
##  b    0.5 (1.0)       0.5 (1.0)       0.503  
##  c                                    0.485  
##     1 25.1%           24.9%                  
##     2 24.6%           23.4%                  
##     3 23.5%           21.8%                  
##     4 26.7%           29.9%                  
## ──────────────────────────────────────────────

Variable Names

You can also adjust the variable names from within the function as so:

## 
## ──────────────────────────────────────────────
##                        d 
##       0               1               P-Value
##       n = 3295        n = 3315               
##  Avar                                 0.786  
##       4949.8 (2868.8) 4969.0 (2884.4)        
##  Bvar                                 0.112  
##       0.5 (1.0)       0.5 (1.0)              
##  Cvar                                 0.414  
##     1 665 (25.3%)     639 (24.1%)            
##     2 652 (24.8%)     677 (25.6%)            
##     3 645 (24.6%)     622 (23.5%)            
##     4 663 (25.3%)     709 (26.8%)            
## ──────────────────────────────────────────────

This is particularly useful when you adjust a variable within the function:

## 
## ──────────────────────────────────────
##                    d 
##       0           1           P-Value
##       n = 3295    n = 3315           
##  A                            1      
##     0 0 (0%)      1 (0%)             
##     1 3295 (100%) 3314 (100%)        
##  b                            0.112  
##       0.5 (1.0)   0.5 (1.0)          
##  c                            0.414  
##     1 665 (25.3%) 639 (24.1%)        
##     2 652 (24.8%) 677 (25.6%)        
##     3 645 (24.6%) 622 (23.5%)        
##     4 663 (25.3%) 709 (26.8%)        
## ──────────────────────────────────────

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....

For more complex naming (e.g., using spacing, brackets, or other values not allowed as name in a data.frame), use the var_names argument.

## 
## ────────────────────────────────────────────────
##                              d 
##                 0           1           P-Value
##                 n = 3295    n = 3315           
##  A New Variable                         1      
##     0           0 (0%)      1 (0%)             
##     1           3295 (100%) 3314 (100%)        
##  B Variable                             0.112  
##                 0.5 (1.0)   0.5 (1.0)          
##  C Variable                             0.414  
##     1           665 (25.3%) 639 (24.1%)        
##     2           652 (24.8%) 677 (25.6%)        
##     3           645 (24.6%) 622 (23.5%)        
##     4           663 (25.3%) 709 (26.8%)        
## ────────────────────────────────────────────────

Final Note

As a final note, the "table1" object can be coerced to a data.frame very easily:

##    Table1..        Table1.0        Table1.1 Table1.P.Value
## 1                  n = 3295        n = 3315               
## 2         a                                          0.786
## 3           4949.8 (2868.8) 4969.0 (2884.4)               
## 4         b                                          0.112
## 5                 0.5 (1.0)       0.5 (1.0)               
## 6         c                                          0.414
## 7         1     665 (25.3%)     639 (24.1%)               
## 8         2     652 (24.8%)     677 (25.6%)               
## 9         3     645 (24.6%)     622 (23.5%)               
## 10        4     663 (25.3%)     709 (26.8%)

Conclusions

table1 can be a valuable addition to the tools that are being utilized to analyze descriptive statistics. Enjoy this valuable piece of furniture!