knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)
devtools::load_all()

CRAN Status Badge Downloads R-CMD-check

furniture: v1.10.0

The furniture R package contains functions to help with data cleaning/tidying (e.g., washer(), rowmeans(), rowsums()), exploratory data analysis and reporting (e.g., table1(), tableC(), tableF()). It currently contains eight main functions:

  1. table1() : gives a well-formatted table for academic publication of descriptive statistics. Very useful for quick analyses as well. Notably, table1() now works with dplyr::group_by().
  2. tableC() : gives a well-formatted table of correlations.
  3. tableF() : provides a thorough frequency table for quick checks of the levels of a variable.
  4. washer() : changes several values in a variable (very useful for changing place holder values to missing).
  5. long() : is a wrapper of stats::reshape(), takes the data from wide to long format (long is often the tidy version of the data), works well with the tidyverse, and can handle unbalanced multilevel data.
  6. wide() : also a wrapper of stats::reshape(), takes the data from long to wide, and like long(), works well with the tidyverse and can handle unbalanced multilevel data.
  7. rowmeans() and rowmeans.n() : tidyverse friendly versions of rowMeans(), where the rowmeans.n() function allows n number of missing
  8. rowsums() and rowsums.n() : tidyverse friendly versions of rowSums(), where the rowsums.n() function allows n number of missing

In conjunction with many other tidy tools, the package should be useful for health, behavioral, and social scientists working on quantitative research.

Installation

The latest stable build of the package can be downloaded from CRAN via:

install.packages("furniture")

You can download the developmental version via:

remotes::install_github("tysonstanley/furniture")

Using furniture

The main functions are the table*() functions (e.g., table1(), tableC(), tableF()).

library(furniture)
data("nhanes_2010")

table1(nhanes_2010,
       age, marijuana, illicit, rehab,
       splitby=~asthma,
       na.rm = FALSE)
table1(nhanes_2010,
       age, marijuana, illicit, rehab,
       splitby=~asthma, 
       output = "text2",
       na.rm = FALSE)
library(tidyverse)
nhanes_2010 %>%
  group_by(asthma) %>%
  table1(age, marijuana, illicit, rehab,
         output = "text2",
         na.rm = FALSE)

table1() can do bivariate significance tests as well.

library(tidyverse)
nhanes_2010 %>%
  group_by(asthma) %>%
  table1(age, marijuana, illicit, rehab,
         output = "text2",
         na.rm = FALSE,
         test = TRUE)

By default it does the appropriate parametric tests. However, you can change that by setting param = FALSE (new with v 1.9.1).

library(tidyverse)
nhanes_2010 %>%
  group_by(asthma) %>%
  table1(age, marijuana, illicit, rehab,
         output = "text2",
         na.rm = FALSE,
         test = TRUE,
         param = FALSE,
         type = "condense")

It can also do a total column with the stratified columns (new with v 1.9.0) with the total = TRUE argument.

nhanes_2010 %>%
  group_by(asthma) %>%
  table1(age, marijuana, illicit, rehab,
         output = "text2",
         na.rm = FALSE,
         test = TRUE,
         type = "condense",
         total = TRUE)

It can also report the statistics in addition to the p-values.

nhanes_2010 %>%
  group_by(asthma) %>%
  table1(age, marijuana, illicit, rehab,
         output = "text2",
         na.rm = FALSE,
         test = TRUE,
         total = TRUE,
         type = "full")

table1() can be outputted directly to other formats. All knitr::kable() options are available for this and there is an extra option "latex2" which provides a publication ready table in Latex documents.

A new function table1_gt() integrates the fantastic gt package to make beautiful HTML tables for table1.

nhanes_2010 %>% 
  group_by(asthma) %>% 
  table1(age, marijuana, illicit, rehab, na.rm = FALSE) %>% 
  table1_gt() %>% 
  print()

The tableC() function gives a well-formatted correlation table.

tableC(nhanes_2010, 
       age, active, vig_active, 
       na.rm=TRUE)

The tableF() function gives a table of frequencies.

tableF(nhanes_2010, age)

In addition, the rowmeans() and rowsums() functions offer a simplified use of rowMeans() and rowSums(), particularly when using the tidyverse's mutate().

nhanes_2010 %>%
  select(vig_active, mod_active) %>%
  mutate(avg_active = rowmeans(vig_active, mod_active, na.rm=TRUE)) %>%
  mutate(sum_active = rowsums(vig_active, mod_active, na.rm=TRUE)) %>% 
  head()

The rowmeans.n() and rowsums.n() allow n missing values while still calculating the mean or sum.

df <- data.frame(
  x = c(NA, 1:5),
  y = c(1:5, NA)
)

df %>%
  mutate(avg = rowmeans.n(x, y, n = 1))

Notes

The package is most useful in conjunction with other tidy tools to get data cleaned/tidied and start exploratory data analysis. I recommend using packages such as library(dplyr), library(tidyr), and library(ggplot2) with library(furniture) to accomplish this.

The original function--table1()--is simply built for both exploratory descriptive analysis and communication of findings. See vignettes or tysonbarrett.com for several examples of its use. Also see our paper in the R Journal.



TysonStanley/furniture documentation built on Feb. 15, 2024, 11:40 p.m.