I would like to use the R function tableby() to get the strata printed as extra columns instead of extra rows, or find another way to accomplish the same thing. I show an example with the mockstudy data below. However, my actual dataset has many more variables (both categorical and numeric), so the different format makes much more sense. Plus, I want to show the number of observations in each subcategory (e.g., Treatment Arm A and Male, Treatment Arm A and Female, etc.).
As a short example, I have the following code in an Rmardown file to generate a table with summary statistics using the tableby() function from the arsenal package in R.
knitr::opts_chunk$set(echo = TRUE)
library(arsenal) # for tableby()
require(knitr)
data(mockstudy) # load data
summary(tableby(sex ~ age + bmi, strata = arm, subset = arm %in% c("A: IFL", "F: FOLFOX"), data = mockstudy))
Which generates the following output:
However, I would like a table arranged in the following way, with the correct N at the top of each column:
Is there a way to do this with tableby() or another function in another R package?
With arsenal and tableby(), I believe your strata argument (strata=arm
) is the best way to do it.
Instead, I’d recommend looking into something like tblstrata() in the gtsummary package; it would give you something that looks like this: Example from gtsummary documentation.
Using the same example you gave, the code would be:
table_example <-
mockstudy %>%
tbl_strata(
strata = arm,
.tbl_fun =
~ .x %>%
tbl_summary(by = sex, missing = "no") %>%
add_n(),
.header = "**{strata}**, N = {n}"
)