Creates a Table1 for Descriptive Statistics with tidyverse utils
devtools::install_github("cities-lab/tidytable1")
library(tidytable1)
library(dplyr)
library(pander)
iris_tbl1 <- tidytable1(iris,
calc_cols=list(n=function(x) length(x)),
num_cols=c(min=min, median=median, max=max)) %>%
pp_table1(pp_vars=c("n"))
iris_tbl1 %>%
pander(missing="")
Name | min | median | max | Category | freq | % | n |
---|---|---|---|---|---|---|---|
Sepal.Length | 4.3 | 5.8 | 7.9 | 150 | |||
Sepal.Width | 2 | 3 | 4.4 | 150 | |||
Petal.Length | 1 | 4.35 | 6.9 | 150 | |||
Petal.Width | 0.1 | 1.3 | 2.5 | 150 | |||
Species | 150 | ||||||
setosa | 50 | 33.33 | |||||
versicolor | 50 | 33.33 | |||||
virginica | 50 | 33.33 |
library(htmlTable)
# tidytable1 differentiate numeric and non-numeric variables
mtcars_df <- mtcars %>%
mutate(cyl=factor(cyl), vs=factor(vs), am=factor(am, labels = c("automatic", "manual"))) %>%
as_tibble()
num_vars <- mtcars_df %>% select_if(is.numeric) %>% names
cat_vars <- mtcars_df %>% select_if(~!is.numeric(.)) %>% names
row_order <- list(Name=c(num_vars, cat_vars))
## organized variables by numeric + categorical
mtcars_tbl1 <- tidytable1(mtcars_df,
info_cols=list(Label=c(mpg="Miles/(US) gallon", cyl="Number of cylinders",
disp="Displacement (cu.in.)", hp="Gross horsepower",
drat="Rear axle ratio", wt="Weight (1000 lbs)",
qsec="1/4 mile time", vs="V/S",
am="Transmission", gear="Number of forward gears",
carb="Number of carburetors"),
Source=c(mpg="1974 Motor Trend")),
num_cols=c(mean=mean, sd=sd),
row_order = row_order) %>%
pp_table1(pp_vars=c("Label", "Source", "#missing"))
mtcars_tbl1 %>%
htmlTable(rnames = FALSE,
align="lll rrc rrr",
rgroup = c("Numeric Variables", "Categorical Variables"),
n.rgroup = c(length(num_vars), length(cat_vars)))
Name | Label | Source | mean | sd | Category | freq | % | \#missing |
---|---|---|---|---|---|---|---|---|
Numeric Variables | ||||||||
mpg | Miles/(US) gallon | 1974 Motor Trend | 20.09 | 6.03 | 0 | |||
disp | Displacement (cu.in.) | 230.72 | 123.94 | 0 | ||||
hp | Gross horsepower | 146.69 | 68.56 | 0 | ||||
drat | Rear axle ratio | 3.6 | 0.53 | 0 | ||||
wt | Weight (1000 lbs) | 3.22 | 0.98 | 0 | ||||
qsec | 1/4 mile time | 17.85 | 1.79 | 0 | ||||
gear | Number of forward gears | 3.69 | 0.74 | 0 | ||||
carb | Number of carburetors | 2.81 | 1.62 | 0 | ||||
Categorical Variables | ||||||||
cyl | Number of cylinders | 0 | ||||||
4 | 11 | 34.38 | ||||||
6 | 7 | 21.88 | ||||||
8 | 14 | 43.75 | ||||||
vs | V/S | 0 | ||||||
0 | 18 | 56.25 | ||||||
1 | 14 | 43.75 | ||||||
am | Transmission | 0 | ||||||
automatic | 19 | 59.38 | ||||||
manual | 13 | 40.62 |
- Enable stats by a grouping variable similar the behavior in
tableone
- Add hypothesis testing functionality