lyt <- basic_table() %>%
split_cols_by("Species") %>%
analyze("Sepal.Length", afun = function(x) {
list(
"mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
"range" = diff(range(x))
)
})
lyt
tbl <- build_table(lyt, iris)
tbl
# analyze multiple variables
lyt2 <- basic_table() %>%
split_cols_by("Species") %>%
analyze(c("Sepal.Length", "Petal.Width"), afun = function(x) {
list(
"mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
"range" = diff(range(x))
)
})
tbl2 <- build_table(lyt2, iris)
tbl2
# an example more relevant for clinical trials with column counts
lyt3 <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ARM") %>%
analyze("AGE", afun = function(x) {
setNames(as.list(fivenum(x)), c(
"minimum", "lower-hinge", "median",
"upper-hinge", "maximum"
))
})
tbl3 <- build_table(lyt3, DM)
tbl3
tbl4 <- build_table(lyt3, subset(DM, AGE > 40))
tbl4
# with column counts calculated based on different data
miniDM <- DM[sample(1:NROW(DM), 100), ]
tbl5 <- build_table(lyt3, DM, alt_counts_df = miniDM)
tbl5
tbl6 <- build_table(lyt3, DM, col_counts = 1:3)
tbl6
Run the code above in your browser using DataLab