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

Merge tbl_hierarchical() and  tbl_hierarchical_count() #2086

Open
p-lemercier opened this issue Nov 28, 2024 · 2 comments
Open

Merge tbl_hierarchical() and  tbl_hierarchical_count() #2086

p-lemercier opened this issue Nov 28, 2024 · 2 comments

Comments

@p-lemercier
Copy link

Hi,
I want to generate hierarchical tables with both adverse event incidence rates and counts. Thus I tried to merge the results of tbl_hierarchical() and  tbl_hierarchical_count() but the resulting table is no longer hierachically ordered.

See examples below:

library(gtsummary)

ADAE_subset <- cards::ADAE |>
  dplyr::filter(
    AESOC %in% unique(cards::ADAE$AESOC)[1:5],
    AETERM %in% unique(cards::ADAE$AETERM)[1:5]
  )

# Event Rates --------------------
evt_rate <- tbl_hierarchical(
  data = ADAE_subset,
  variables = c(AESOC, AETERM),
  by = TRTA,
  denominator = cards::ADSL |> mutate(TRTA = ARM),
  id = USUBJID
)

# Event Counts -------------------
evt_cts <- tbl_hierarchical_count(
  data = ADAE_subset,
  variables = c(AESOC, AETERM),
  by = TRTA
)

evt_merge <- tbl_merge(
  list(evt_rate, evt_cts),
  tab_spanner = c("Rates", "Counts")
  )

evt_merge

image

I managed to reorder it manually this way :

evt_merge$table_body <- evt_merge$table_body |>
  dplyr::arrange(factor(label, levels = evt_rate$table_body$label))

evt_merge

image

I think tbl_merge should be able to manage hierarchical tables.

Thanks!

@ddsjoberg
Copy link
Owner

For sure, we'll get to this soon. Thanks for the post @p-lemercier !

@ddsjoberg
Copy link
Owner

ddsjoberg commented Nov 28, 2024

NOTE TO SELF: We just need to update tbl_merge() to include grouping variables and levels in the variable-wise merging to retain the order. OR we could just recommend tbl_cbind() (#1855)

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

2 participants