Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twoway summary tables #115

Open
Isholasky opened this issue Nov 16, 2023 · 5 comments
Open

Twoway summary tables #115

Isholasky opened this issue Nov 16, 2023 · 5 comments

Comments

@Isholasky
Copy link

Isholasky commented Nov 16, 2023

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
Copy link
Owner

benjaminrich commented Nov 16, 2023

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
Copy link
Author

Isholasky commented Nov 17, 2023

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
Copy link

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
Copy link
Owner

@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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants