Furniture

2023-09-04

This vignette is current as of furniture 1.9.14.

Using furniture

library(furniture)

We will first make a fictitious data set:

df <- data.frame(a = rnorm(1000, 1.5, 2), 
                 b = seq(1, 1000, 1), 
                 c = c(rep("control", 400), rep("Other", 70), rep("treatment", 500), rep("None", 30)),
                 d = c(sample(1:1000, 900, replace=TRUE), rep(-99, 100)))

There are four functions that we’ll demonstrate here:

  1. washer
  2. table1
  3. tableC
  4. tableF

Washer

washer is a great function for quick data cleaning. In situations where there are placeholders, extra levels in a factor, or several values need to be changed to another.

library(dplyr)

df <- df %>%
  mutate(d = washer(d, -99),  ## changes the placeholder -99 to NA
         c = washer(c, "Other", "None", value = "control")) ## changes "Other" and "None" to "Control"

Table1

Now that the data is “washed” we can start exploring and reporting.

table1(df, a, b, factor(c), d)
## 
## ────────────────────────────────
##               Mean/Count (SD/%)
##               n = 900          
##  a                             
##               1.5 (2.0)        
##  b                             
##               450.5 (260.0)    
##  factor(c)                     
##     control   470 (52.2%)      
##     treatment 430 (47.8%)      
##  d                             
##               500.3 (293.0)    
## ────────────────────────────────

The variables must be numeric or factor. Since we use a special type of evaluation (i.e. Non-Standard Evaluation) we can change the variables in the function (e.g., factor(c)). This can be extended to making a whole new variable in the function as well.

table1(df, a, b, d, ifelse(a > 1, 1, 0))
## 
## ───────────────────────────────────────
##                      Mean/Count (SD/%)
##                      n = 900          
##  a                                    
##                      1.5 (2.0)        
##  b                                    
##                      450.5 (260.0)    
##  d                                    
##                      500.3 (293.0)    
##  ifelse(a > 1, 1, 0)                  
##                      0.6 (0.5)        
## ───────────────────────────────────────

This is just the beginning though. Two powerful things the function can do are shown below:

table1(df, a, b, d, ifelse(a > 1, 1, 0),
       splitby=~factor(c), 
       test=TRUE)
## Breusch-Pagan Test of Heteroskedasticity suggests `var.equal = FALSE` in t.test() for: b
## 
## ─────────────────────────────────────────────────────────
##                             factor(c) 
##                      control       treatment     P-Value
##                      n = 470       n = 430              
##  a                                               0.364  
##                      1.4 (2.0)     1.6 (2.0)            
##  b                                               <.001  
##                      235.5 (135.8) 685.5 (124.3)        
##  d                                               0.567  
##                      505.7 (288.3) 494.5 (298.2)        
##  ifelse(a > 1, 1, 0)                             0.448  
##                      0.6 (0.5)     0.6 (0.5)            
## ─────────────────────────────────────────────────────────

The splitby = ~factor(c) stratifies the means and counts by a factor variable (in this case either control or treatment). When we use this we can also automatically compute tests of significance using test=TRUE.

We can also use it intuitively within the pipe (for more about this, see the “Table 1” vignette):

df %>%
  group_by(c) %>%
  table1(a, b, d, ifelse(a > 1, 1, 0), 
        test=TRUE)
## 
## ─────────────────────────────────────────────────────────
##                                 c 
##                      control       treatment     P-Value
##                      n = 470       n = 430              
##  a                                               0.364  
##                      1.4 (2.0)     1.6 (2.0)            
##  b                                               <.001  
##                      235.5 (135.8) 685.5 (124.3)        
##  d                                               0.567  
##                      505.7 (288.3) 494.5 (298.2)        
##  ifelse(a > 1, 1, 0)                             0.448  
##                      0.6 (0.5)     0.6 (0.5)            
## ─────────────────────────────────────────────────────────

In this case, we used the group_by() function from dplyr (within the tidyverse) and table1() knows to use that as the grouping variable in place of the splitby argument.

If the parametric tests (default) are not appropriate, you can set param = FALSE.

df %>%
  group_by(c) %>%
  table1(a, b, d, ifelse(a > 1, 1, 0), 
        test=TRUE,
        param=FALSE)
## 
## ─────────────────────────────────────────────────────────
##                                 c 
##                      control       treatment     P-Value
##                      n = 470       n = 430              
##  a                                               0.292  
##                      1.4 (2.0)     1.6 (2.0)            
##  b                                               <.001  
##                      235.5 (135.8) 685.5 (124.3)        
##  d                                               0.555  
##                      505.7 (288.3) 494.5 (298.2)        
##  ifelse(a > 1, 1, 0)                             0.448  
##                      0.6 (0.5)     0.6 (0.5)            
## ─────────────────────────────────────────────────────────

Finally, you can polish it quite a bit using a few other options. For example, you can do the following:

table1(df, a, b, d, ifelse(a > 1, 1, 0),
       splitby=~factor(c), 
       test=TRUE,
       var_names = c("A", "B", "D", "New Var"),
       type = c("simple", "condensed"))
## Breusch-Pagan Test of Heteroskedasticity suggests `var.equal = FALSE` in t.test() for: B
## 
## ─────────────────────────────────────────────
##                 factor(c) 
##          control       treatment     P-Value
##          n = 470       n = 430              
##  A       1.4 (2.0)     1.6 (2.0)     0.364  
##  B       235.5 (135.8) 685.5 (124.3) <.001  
##  D       505.7 (288.3) 494.5 (298.2) 0.567  
##  New Var 0.6 (0.5)     0.6 (0.5)     0.448  
## ─────────────────────────────────────────────

Note that var_names can be used for more complex naming (e.g., with spaces, brackets) that otherwise cannot be used with data frames. Alternatively, for more simple naming, we can name them directly.

table1(df, A = a, B = b, D = d, A2 = ifelse(a > 1, 1, 0),
       splitby=~factor(c), 
       test=TRUE,
       type = c("simple", "condensed"))
## Breusch-Pagan Test of Heteroskedasticity suggests `var.equal = FALSE` in t.test() for: B
## 
## ────────────────────────────────────────
##            factor(c) 
##     control       treatment     P-Value
##     n = 470       n = 430              
##  A  1.4 (2.0)     1.6 (2.0)     0.364  
##  B  235.5 (135.8) 685.5 (124.3) <.001  
##  D  505.7 (288.3) 494.5 (298.2) 0.567  
##  A2 0.6 (0.5)     0.6 (0.5)     0.448  
## ────────────────────────────────────────

You can also format the numbers (adding a comma for big numbers such as in 20,000 instead of 20000):

table1(df, a, b, d, ifelse(a > 1, 1, 0),
       splitby=~factor(c), 
       test=TRUE,
       var_names = c("A", "B", "D", "New Var"),
       format_number = TRUE)
## Breusch-Pagan Test of Heteroskedasticity suggests `var.equal = FALSE` in t.test() for: B
## 
## ─────────────────────────────────────────────
##                 factor(c) 
##          control       treatment     P-Value
##          n = 470       n = 430              
##  A                                   0.364  
##          1.4 (2.0)     1.6 (2.0)            
##  B                                   <.001  
##          235.5 (135.8) 685.5 (124.3)        
##  D                                   0.567  
##          505.7 (288.3) 494.5 (298.2)        
##  New Var                             0.448  
##          0.6 (0.5)     0.6 (0.5)            
## ─────────────────────────────────────────────

The table can be exported directly to a folder in the working directory called “Table1”. Using export, we provide it with a string that will be the name of the CSV containing the formatted table.

table1(df, a, b, d, ifelse(a > 1, 1, 0),
       splitby=~factor(c), 
       test=TRUE,
       var_names = c("A", "B", "D", "New Var"),
       format_number = TRUE,
       export = "example_table1")

This can also be outputted as a latex, markdown, or pandoc table (matching all the output types of knitr::kable). Below shows how to do a latex table (not using kable however, but a built-in function that provides the variable name at the top of the table):

table1(df, a, b, d, "new var" = ifelse(a > 1, 1, 0),
       splitby = ~factor(c), 
       test = TRUE,
       output = "latex2")
## Breusch-Pagan Test of Heteroskedasticity suggests `var.equal = FALSE` in t.test() for: b
## \begin{table}[ ht ] 
## \centering 
## \caption{}\label{}
## \begin{tabular}{ l c c c }
## \toprule
##  &   \multicolumn{ 2 }{c}{ factor(c) }\\ 
##   & control & treatment & P-Value \\ 
##   & n = 470 & n = 430 &   \\ 
##  \midrule
## a &   &   & 0.364\\ 
## \hspace{6pt}   & 1.4 (2.0) & 1.6 (2.0) & \\ 
## b &   &   & <.001\\ 
## \hspace{6pt}   & 235.5 (135.8) & 685.5 (124.3) & \\ 
## d &   &   & 0.567\\ 
## \hspace{6pt}   & 505.7 (288.3) & 494.5 (298.2) & \\ 
## new var &   &   & 0.448\\ 
## \hspace{6pt}   & 0.6 (0.5) & 0.6 (0.5) & \\ 
## \bottomrule
## 
## \end{tabular}
## \end{table}

Last item to show you regarding table1() is that it can be printed in a simplified and condensed form. This instead of reporting counts and percentages for categorical variables, it reports only percentages and the table has much less white space.

table1(df, a, b, d, "new var" = ifelse(a > 1, 1, 0),
       splitby = ~factor(c), 
       test = TRUE,
       type = c("simple", "condensed"))
## Breusch-Pagan Test of Heteroskedasticity suggests `var.equal = FALSE` in t.test() for: b
## 
## ─────────────────────────────────────────────
##                 factor(c) 
##          control       treatment     P-Value
##          n = 470       n = 430              
##  a       1.4 (2.0)     1.6 (2.0)     0.364  
##  b       235.5 (135.8) 685.5 (124.3) <.001  
##  d       505.7 (288.3) 494.5 (298.2) 0.567  
##  new var 0.6 (0.5)     0.6 (0.5)     0.448  
## ─────────────────────────────────────────────

Table C

This function is to create simple, beautiful correlation tables. The syntax is just like table1() in most respects. Below we include all the numeric variables to see their correlations. Since there are missing values in d we will use the natural na.rm=TRUE.

tableC(df, 
       a, b, d,
       na.rm = TRUE)
## N = 900
## Note: pearson correlation (p-value).
## 
## ─────────────────────────────────────────
##       [1]           [2]            [3]  
##  [1]a 1.00                              
##  [2]b 0.041 (0.215) 1.00                
##  [3]d 0.009 (0.781) -0.069 (0.038) 1.00 
## ─────────────────────────────────────────

All the adjustments that you can make in table1() can be done here as well. For example,

tableC(df, 
       "A" = a, "B" = b, "D" = d,
       na.rm = TRUE,
       output = "html")
## N = 900
## Note: pearson correlation (p-value).
[1] [2] [3]
[1]A 1.00
[2]B 0.041 (0.215) 1.00
[3]D 0.009 (0.781) -0.069 (0.038) 1.00

Table F

This function is to create simple frequency tables. The syntax is just like table1() and tableC() in most respects, except that it uses only one variable instead of many.

tableF(df, a)
## 
## ────────────────────────────────────────────────
##  a                 Freq CumFreq Percent CumPerc
##  -4.63163278530117 1    1       0.10%   0.10%  
##  -4.06865421576969 1    2       0.10%   0.20%  
##  -3.84527844203444 1    3       0.10%   0.30%  
##  -3.58563837197359 1    4       0.10%   0.40%  
##  -3.53767477384542 1    5       0.10%   0.50%  
##  -3.28913227159756 1    6       0.10%   0.60%  
##  -3.22087461844618 1    7       0.10%   0.70%  
##  -3.19007978696343 1    8       0.10%   0.80%  
##  -3.15275096597904 1    9       0.10%   0.90%  
##  -3.09564394452743 1    10      0.10%   1.00%  
##  ...               ...  ...     ...     ...    
##  5.96116427060756  1    990     0.10%   99.00% 
##  6.01414503213269  1    991     0.10%   99.10% 
##  6.08524349579695  1    992     0.10%   99.20% 
##  6.17036294175038  1    993     0.10%   99.30% 
##  6.41610434542787  1    994     0.10%   99.40% 
##  6.52558785448728  1    995     0.10%   99.50% 
##  6.6528967541495   1    996     0.10%   99.60% 
##  6.67211204808162  1    997     0.10%   99.70% 
##  6.819841180341    1    998     0.10%   99.80% 
##  7.45602363746625  1    999     0.10%   99.90% 
##  8.41647680004542  1    1000    0.10%   100.00%
## ────────────────────────────────────────────────

Similarly to table1() we can use a splitby argument (or group_by()).

tableF(df, d, splitby = c)
## Variable:d
## 
## ─────────────────────────────────────────────────────
##  control Freq CumFreq Percent CumPerc Valid CumValid
##  1       3    3       0.60%   0.60%   0.64% 0.64%   
##  11      1    4       0.20%   0.80%   0.21% 0.85%   
##  16      1    5       0.20%   1.00%   0.21% 1.06%   
##  19      1    6       0.20%   1.20%   0.21% 1.28%   
##  20      1    7       0.20%   1.40%   0.21% 1.49%   
##  23      1    8       0.20%   1.60%   0.21% 1.70%   
##  28      1    9       0.20%   1.80%   0.21% 1.91%   
##  30      1    10      0.20%   2.00%   0.21% 2.13%   
##  31      1    11      0.20%   2.20%   0.21% 2.34%   
##  34      1    12      0.20%   2.40%   0.21% 2.55%   
##  ...     ...  ...     ...     ...     ...   ...     
##  970     2    459     0.40%   91.80%  0.43% 97.66%  
##  976     2    461     0.40%   92.20%  0.43% 98.09%  
##  978     1    462     0.20%   92.40%  0.21% 98.30%  
##  979     1    463     0.20%   92.60%  0.21% 98.51%  
##  986     1    464     0.20%   92.80%  0.21% 98.72%  
##  990     1    465     0.20%   93.00%  0.21% 98.94%  
##  993     2    467     0.40%   93.40%  0.43% 99.36%  
##  994     1    468     0.20%   93.60%  0.21% 99.57%  
##  997     1    469     0.20%   93.80%  0.21% 99.79%  
##  1000    1    470     0.20%   94.00%  0.21% 100.00% 
##  Missing 30   500     6.00%   100.00%               
## ─────────────────────────────────────────────────────
## 
## ───────────────────────────────────────────────────────
##  treatment Freq CumFreq Percent CumPerc Valid CumValid
##  2         1    1       0.20%   0.20%   0.23% 0.23%   
##  3         2    3       0.40%   0.60%   0.47% 0.70%   
##  6         2    5       0.40%   1.00%   0.47% 1.16%   
##  8         1    6       0.20%   1.20%   0.23% 1.40%   
##  9         1    7       0.20%   1.40%   0.23% 1.63%   
##  11        1    8       0.20%   1.60%   0.23% 1.86%   
##  13        1    9       0.20%   1.80%   0.23% 2.09%   
##  16        1    10      0.20%   2.00%   0.23% 2.33%   
##  19        1    11      0.20%   2.20%   0.23% 2.56%   
##  22        1    12      0.20%   2.40%   0.23% 2.79%   
##  ...       ...  ...     ...     ...     ...   ...     
##  980       1    420     0.20%   84.00%  0.23% 97.67%  
##  981       1    421     0.20%   84.20%  0.23% 97.91%  
##  984       1    422     0.20%   84.40%  0.23% 98.14%  
##  985       1    423     0.20%   84.60%  0.23% 98.37%  
##  987       1    424     0.20%   84.80%  0.23% 98.60%  
##  992       2    426     0.40%   85.20%  0.47% 99.07%  
##  995       1    427     0.20%   85.40%  0.23% 99.30%  
##  996       1    428     0.20%   85.60%  0.23% 99.53%  
##  997       1    429     0.20%   85.80%  0.23% 99.77%  
##  1000      1    430     0.20%   86.00%  0.23% 100.00% 
##  Missing   70   500     14.00%  100.00%               
## ───────────────────────────────────────────────────────
df %>%
  group_by(c) %>%
  tableF(d)
## Using dplyr::group_by() groups: c
## Variable:d
## 
## ─────────────────────────────────────────────────────
##  control Freq CumFreq Percent CumPerc Valid CumValid
##  1       3    3       0.60%   0.60%   0.64% 0.64%   
##  11      1    4       0.20%   0.80%   0.21% 0.85%   
##  16      1    5       0.20%   1.00%   0.21% 1.06%   
##  19      1    6       0.20%   1.20%   0.21% 1.28%   
##  20      1    7       0.20%   1.40%   0.21% 1.49%   
##  23      1    8       0.20%   1.60%   0.21% 1.70%   
##  28      1    9       0.20%   1.80%   0.21% 1.91%   
##  30      1    10      0.20%   2.00%   0.21% 2.13%   
##  31      1    11      0.20%   2.20%   0.21% 2.34%   
##  34      1    12      0.20%   2.40%   0.21% 2.55%   
##  ...     ...  ...     ...     ...     ...   ...     
##  970     2    459     0.40%   91.80%  0.43% 97.66%  
##  976     2    461     0.40%   92.20%  0.43% 98.09%  
##  978     1    462     0.20%   92.40%  0.21% 98.30%  
##  979     1    463     0.20%   92.60%  0.21% 98.51%  
##  986     1    464     0.20%   92.80%  0.21% 98.72%  
##  990     1    465     0.20%   93.00%  0.21% 98.94%  
##  993     2    467     0.40%   93.40%  0.43% 99.36%  
##  994     1    468     0.20%   93.60%  0.21% 99.57%  
##  997     1    469     0.20%   93.80%  0.21% 99.79%  
##  1000    1    470     0.20%   94.00%  0.21% 100.00% 
##  Missing 30   500     6.00%   100.00%               
## ─────────────────────────────────────────────────────
## 
## ───────────────────────────────────────────────────────
##  treatment Freq CumFreq Percent CumPerc Valid CumValid
##  2         1    1       0.20%   0.20%   0.23% 0.23%   
##  3         2    3       0.40%   0.60%   0.47% 0.70%   
##  6         2    5       0.40%   1.00%   0.47% 1.16%   
##  8         1    6       0.20%   1.20%   0.23% 1.40%   
##  9         1    7       0.20%   1.40%   0.23% 1.63%   
##  11        1    8       0.20%   1.60%   0.23% 1.86%   
##  13        1    9       0.20%   1.80%   0.23% 2.09%   
##  16        1    10      0.20%   2.00%   0.23% 2.33%   
##  19        1    11      0.20%   2.20%   0.23% 2.56%   
##  22        1    12      0.20%   2.40%   0.23% 2.79%   
##  ...       ...  ...     ...     ...     ...   ...     
##  980       1    420     0.20%   84.00%  0.23% 97.67%  
##  981       1    421     0.20%   84.20%  0.23% 97.91%  
##  984       1    422     0.20%   84.40%  0.23% 98.14%  
##  985       1    423     0.20%   84.60%  0.23% 98.37%  
##  987       1    424     0.20%   84.80%  0.23% 98.60%  
##  992       2    426     0.40%   85.20%  0.47% 99.07%  
##  995       1    427     0.20%   85.40%  0.23% 99.30%  
##  996       1    428     0.20%   85.60%  0.23% 99.53%  
##  997       1    429     0.20%   85.80%  0.23% 99.77%  
##  1000      1    430     0.20%   86.00%  0.23% 100.00% 
##  Missing   70   500     14.00%  100.00%               
## ───────────────────────────────────────────────────────

Table X

Lastly, tableX() is a pipe-able two-way version of table() with a similar syntax to that of the rest of the furniture functions.

df %>%
  tableX(c, ifelse(d > 500, 1, 0))
##            ifelse(d > 500, 1, 0)
## c           0   1   Missing Total
##   control   225 245 30      500  
##   treatment 215 215 70      500  
##   Total     440 460 100     1000

By default, it provides the total counts for the rows and columns with flexibility as to what is displayed and where.

Conclusion

The four functions: table1(), tableC(), tableF(), and washer() add simplicity to cleaning up and understanding your data. Use these pieces of furniture to make your quantitative life a bit easier.