Skip to content

Instantly share code, notes, and snippets.

@samuelsaari
Last active April 28, 2022 14:40
Show Gist options
  • Save samuelsaari/495239cdcf4ab70dbcadc02d69d4f2c5 to your computer and use it in GitHub Desktop.
Save samuelsaari/495239cdcf4ab70dbcadc02d69d4f2c5 to your computer and use it in GitHub Desktop.
require(devtools)
#devtools::install_github("benjaminrich/table1")
library(boot)
library(table1)
library(kableExtra)
melanoma2 <- melanoma
# Factor the basic variables that
# we're interested in
melanoma2$status <-
factor(melanoma2$status,
levels=c(2,1,3),
labels=c("Alive", # Reference
"Melanoma death",
"Non-melanoma death"))
# table1(~ factor(sex) + age + factor(ulcer) + thickness | status, data=melanoma2)
melanoma2$sex <-
factor(melanoma2$sex, levels=c(1,0),
labels=c("Male",
"Female"))
melanoma2$ulcer <-
factor(melanoma2$ulcer, levels=c(0,1),
labels=c("Absent",
"Present"))
label(melanoma2$sex) <- "Sex"
label(melanoma2$age) <- "Age"
label(melanoma2$ulcer) <- "Ulceration"
label(melanoma2$thickness) <- "Thickness"
units(melanoma2$age) <- "years"
units(melanoma2$thickness) <- "mm"
#table1(~ sex + age + ulcer + thickness | status, data=melanoma2, overall="Total")
labels <- list(
variables=list(sex="Sex",
age="Age (years)",
ulcer="Ulceration",
thickness="Thickness (mm)"),
groups=list("", "", "Death"))
# Remove the word "death" from the labels, since it now appears above
levels(melanoma2$status) <- c("Alive", "Melanoma", "Non-melanoma")
strata <- c(list(Total=melanoma2), split(melanoma2, melanoma2$status))
my.render.cont <- function(x) {
with(stats.apply.rounding(stats.default(x), digits=2), c("",
"Mean (SD)"=sprintf("%s (&plusmn; %s)", MEAN, SD)))
}
my.render.cat <- function(x) {
c("", sapply(stats.default(x), function(y) with(y,
sprintf("%d (%0.0f %%)", FREQ, PCT))))
}
rendered_table1_object <- table1(strata, labels, groupspan=c(1, 1, 2),
render.continuous=my.render.cont, render.categorical=my.render.cat)
rendered_kable <- t1kable(rendered_table1_object ,format='latex')
#Error in rbind(deparse.level, ...) :
# numbers of columns of arguments do not match
save_kable(rendered_kable,'rendered_kable.tex')
# t1kable command would work without the rendering parameters
rendered_table1 <- table1(strata, labels, groupspan=c(1, 1, 2))
##########33
test1 <- table1(strata, labels, groupspan=c(1, 3, 1),
render.continuous=c(.="Mean (CV%)", .="Median [Min, Max]",
"Geo. mean (Geo. CV%)"="GMEAN (GCV%)"))
sessionInfo()
# R version 4.2.0 (2022-04-22 ucrt)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19044)
#
# Matrix products: default
#
# locale:
# [1] LC_COLLATE=Finnish_Finland.utf8
# [2] LC_CTYPE=Finnish_Finland.utf8
# [3] LC_MONETARY=Finnish_Finland.utf8
# [4] LC_NUMERIC=C
# [5] LC_TIME=Finnish_Finland.utf8
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods
# [7] base
#
# other attached packages:
# [1] kableExtra_1.3.4 boot_1.3-28 devtools_2.4.3
# [4] usethis_2.1.5 table1_1.4.2
#
# loaded via a namespace (and not attached):
# [1] compiler_4.2.0 prettyunits_1.1.1 remotes_2.4.2
# [4] tools_4.2.0 testthat_3.1.4 digest_0.6.29
# [7] pkgbuild_1.3.1 pkgload_1.2.4 viridisLite_0.4.0
# [10] evaluate_0.15 memoise_2.0.1 lifecycle_1.0.1
# [13] rlang_1.0.2 cli_3.3.0 rstudioapi_0.13
# [16] curl_4.3.2 xfun_0.30 fastmap_1.1.0
# [19] stringr_1.4.0 xml2_1.3.3 httr_1.4.2
# [22] withr_2.5.0 knitr_1.39 systemfonts_1.0.4
# [25] desc_1.4.1 fs_1.5.2 webshot_0.5.3
# [28] rprojroot_2.0.3 svglite_2.1.0 glue_1.6.2
# [31] R6_2.5.1 processx_3.5.3 rmarkdown_2.14
# [34] sessioninfo_1.2.2 Formula_1.2-4 callr_3.7.0
# [37] purrr_0.3.4 magrittr_2.0.3 scales_1.2.0
# [40] ps_1.7.0 ellipsis_0.3.2 htmltools_0.5.2
# [43] rvest_1.0.2 colorspace_2.0-3 stringi_1.7.6
# [46] munsell_0.5.0 cachem_1.0.6 crayon_1.5.1
# [49] brio_1.1.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment