benjaminrich / table1

78 stars 26 forks source link

Twoway summary tables #115

Open Isholasky opened 7 months ago

Isholasky commented 7 months ago

Hi there, I'm looking for a way to create a two-way table that uses a summary measure to fill the cells. For example, I have 3 variables: gender (factor), region (factor), and score (continuous). I aim to generate a two-way table featuring the factor variables, region and gender, as rows and columns, respectively. But this table should display summary statistics of score, such as mean and standard deviation, as the content of each cell. Any thoughts on how this could be done?

benjaminrich commented 7 months ago

My suggestion for this would actually be to use a different package ;)

In particular, you could try a different package I wrote called ttt. The two packages are independent, but compliment each other and work together to some extent, as in this example:

library(ttt)
library(table1)

options(ttt.theme="booktabs")

# simulate some data for this example
set.seed(123)

n <- 123

dat <- data.frame(
    gender = sample(factor(c("Female", "Male")), n, replace=T),
    region = sample(factor(LETTERS[1:5]),        n, replace=T),
    score  = runif(n)
)

label(dat$region) <- "Region"

rndr <- function(x) {
    table1::render.default(x, render.continuous=c("N", "Mean", "SD"))[-1]
}

ttt(score ~ region | gender, data=dat, render=rndr, lab="Gender")

The result looks like this:

image

Not sure if that is exactly what you are looking, but the package is pretty flexible.

(EDIT: changed theme to booktabs, which looks nicer to me)

Isholasky commented 7 months ago

Thank you for your quick response. This is incredibly useful for my current needs. I'll be sure to read the documentation of the ttt package to improve the look of the table. But if you've got a few times, can you please share with me the commands to have the same table with an added total column and looking a bit like this (format created in Excel)?

image

I really appreciate your support!

ronaldsanchez87 commented 7 months ago

Hi, I am looking to add a second level to a table, to make it look like this: image

I can't seem to add the second column named 'caregiver_child_group'

Any recommendations?

benjaminrich commented 7 months ago

@ronaldsanchez87: This is easy to do with ttt. I don't have your data, but here's an example with data I simulated:

library(ttt)
options(ttt.theme="booktabs")
set.seed(123)

dat <- expand.grid(
    family_id_mrn_count   = 1:3,
    caregiver_child_group = factor(1:2, labels=c("Caregiver", "Child")),
    y                     = factor(1:3, labels=c("OB Post-Natal", "OB Pre-Natal", "Pediatrics"))
)

dat <- dat[sample(1:nrow(dat), 100, replace=T),]

ttt(rep(1, nrow(dat)) ~ family_id_mrn_count + caregiver_child_group | y, data=dat)

image

ronaldsanchez87 commented 7 months ago

This is very helpful, thank you. I realize there are two additional things I need to be able to do. First, add additional variables under the family_id_mrn_count that will also be sub-grouped by caregiver_child_group. Secondly, apply anova and chi-square tests for each combination.