table1 icon indicating copy to clipboard operation
table1 copied to clipboard

Twoway summary tables

Open olaitan-elihou opened this issue 2 years ago • 5 comments

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?

olaitan-elihou avatar Nov 16 '23 15:11 olaitan-elihou

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)

benjaminrich avatar Nov 16 '23 23:11 benjaminrich

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!

olaitan-elihou avatar Nov 17 '23 11:11 olaitan-elihou

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?

ronaldsanchez87 avatar Dec 01 '23 23:12 ronaldsanchez87

@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

benjaminrich avatar Dec 03 '23 15:12 benjaminrich

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.

ronaldsanchez87 avatar Dec 04 '23 21:12 ronaldsanchez87