Data Description
General Data Description
- Create a HTML file the describe all variables in a dataset
tableone
- Use R package tableone to describe variables by strata
library(Wu)
dt <- data.table(
age = sample(x = 18:65, size = 100, replace = TRUE)
, sex = sample(x = c("M", "F", NA), size = 100, replace = TRUE)
, treatment = rep(x = c("Case", "Control"), times = 50)
)
options(knitr.kable.NA = '')
vars <- c("age", "sex")
factorVars <- c("sex")
tbl <- Wu::tbl1n(data = dt
, vars = vars
, factorVars = factorVars
, strata = "treatment"
, test = TRUE
, includeNA = TRUE
, showAllLevels = TRUE
, catDigits = 3
, conDigits = 2
, pDigits = 4
)
tbl %>% Wu::prt()
Variable | level | Overall | Case | Control | p | test | SMD | Missing |
---|---|---|---|---|---|---|---|---|
n | n | 100 | 50 | 50 | ||||
age | mean (SD) | 43.50 (13.90) | 42.88 (13.95) | 44.12 (13.97) | 0.6579 | 0.0888 | 0.0 | |
median [IQR] | 46.00 [31.00, 55.25] | 44.50 [31.00, 54.50] | 47.50 [32.00, 56.00] | 0.6391 | nonnorm | 0.0888 | 0.0 | |
median [range] | 46.00 [18.00, 65.00] | 44.50 [18.00, 65.00] | 47.50 [18.00, 65.00] | 0.6391 | nonnorm | 0.0888 | 0.0 | |
sex (%) | F | 38 (38.000) | 16 (32.000) | 22 (44.000) | 0.4412 | 0.2580 | 34.0 | |
M | 28 (28.000) | 16 (32.000) | 12 (24.000) | |||||
34 (34.000) | 18 (36.000) | 16 (32.000) |
Two Continuous Variables
library(data.table)
dt <- data.table(
outcome = rnorm(100)
)
dt <- dt[, var1 := outcome * 0.3 + rnorm(100) * 0.3
][, var2 := outcome * 0.01 + rnorm(100)]
crrs <- Wu::Table2_nbyn(data = dt, var = "outcome", nvars = c("var1", "var2"), digits_n = 3, digits_p = 4)
crrs %>% prt()
Variable x | Variable y | N | n | Missing | Pearson’s Cor | p value | Spearman’s rho | p value.1 | Variable |
---|---|---|---|---|---|---|---|---|---|
outcome | var1 | 100 | 100 | 0 (0.0%) | 0.711 (0.599, 0.796) | < 0.0001 | 0.635 (0.473, 0.755) | < 0.0001 | |
outcome | var2 | 100 | 100 | 0 (0.0%) | -0.092 (-0.283, 0.107) | 0.3637 | -0.110 (-0.301, 0.090) | 0.2771 |
Continuous Variable by Categories
library(data.table)
dt <- data.table(
treatment = factor(sample(c("Case", "Control"), size = 100, replace = TRUE), levels = c("Case", "Control"))
, var = rnorm(100)
, treatment2 = factor(sample(c("A", "B", "C"), size = 100, replace = TRUE), levels = c("A", "B", "C"))
)
label(dt$treatment) <- "Treatment I"
label(dt$treatment2) <- "Treatment II"
crrs <- Wu::Table2_nbyc(data = dt, group = c("treatment", "treatment2"), var = "var", digits_c = 3, digits_p = 4)
crrs %>% prt()
Variable | Level | N | Missing | Mean(SD) | p(ANOVA) | Median(IQR) | p(KW) | Mode(Range) |
---|---|---|---|---|---|---|---|---|
Treatment I | Case | 48 | 0 (0.0000%) | 0.0445 (1.16) | 0.7625 | 0.102 (-0.833, 0.626) | 0.8145 | 0.2 (-2.1, 3.35) |
Control | 52 | 0 (0.0000%) | -0.0216 (1.02) | -0.261 (-0.67, 0.561) | -0.543 (-2.2, 3.44) | |||
Treatment II | A | 27 | 0 (0.0000%) | 0.0621 (1.04) | 0.8542 | 0.0634 (-0.696, 0.79) | 0.6609 | -0.153 (-2.1, 1.89) |
B | 30 | 0 (0.0000%) | 0.0647 (1.17) | 0.0919 (-0.638, 0.653) | -0.324 (-2.2, 3.44) | |||
C | 43 | 0 (0.0000%) | -0.0606 (1.07) | -0.1 (-0.774, 0.388) | -0.474 (-1.74, 3.35) |
R sessionInfo
R version 4.1.2 (2021-11-01) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] Wu_0.0.0.9000 flexdashboard_0.5.2 lme4_1.1-27.1
[4] Matrix_1.4-0 mgcv_1.8-38 nlme_3.1-152
[7] png_0.1-7 scales_1.1.1 nnet_7.3-16
[10] labelled_2.8.0 kableExtra_1.3.4 plotly_4.9.4.1
[13] gridExtra_2.3 ggplot2_3.3.5 DT_0.18
[16] tableone_0.13.0 magrittr_2.0.3 lubridate_1.7.10
[19] dplyr_1.0.9 plyr_1.8.6 data.table_1.14.2
[22] rmdformats_1.0.2 knitr_1.39
loaded via a namespace (and not attached): [1] httr_1.4.2 sass_0.4.0 tidyr_1.1.3 jsonlite_1.7.2
[5] viridisLite_0.4.0 splines_4.1.2 bslib_0.2.5.1 assertthat_0.2.1 [9] highr_0.9 yaml_2.3.5 pillar_1.7.0 lattice_0.20-45
[13] glue_1.6.2 digest_0.6.29 rvest_1.0.0 minqa_1.2.4
[17] colorspace_2.0-2 htmltools_0.5.2 survey_4.0 pkgconfig_2.0.3
[21] haven_2.4.1 bookdown_0.22 purrr_0.3.4 webshot_0.5.2
[25] svglite_2.0.0 proxy_0.4-26 tibble_3.1.7 generics_0.1.2
[29] ellipsis_0.3.2 withr_2.4.3 klippy_0.0.0.9500 lazyeval_0.2.2
[33] cli_3.3.0 survival_3.2-13 crayon_1.5.1 evaluate_0.15
[37] fansi_1.0.3 MASS_7.3-54 class_7.3-19 forcats_0.5.1
[41] xml2_1.3.3 tools_4.1.2 hms_1.1.0 mitools_2.4
[45] lifecycle_1.0.1 stringr_1.4.0 munsell_0.5.0 e1071_1.7-7
[49] compiler_4.1.2 jquerylib_0.1.4 systemfonts_1.0.2 rlang_1.0.2
[53] grid_4.1.2 nloptr_1.2.2.2 rstudioapi_0.13 htmlwidgets_1.5.4 [57] rmarkdown_2.10 boot_1.3-28 gtable_0.3.0 DBI_1.1.1
[61] R6_2.5.1 zoo_1.8-9 fastmap_1.1.0 utf8_1.2.2
[65] stringi_1.7.6 Rcpp_1.0.8.3 vctrs_0.4.1 tidyselect_1.1.2 [69] xfun_0.31