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

[Bug]: Count number of patient with percentage with row-wise denominator column by column #1355

Closed
huanlugwu opened this issue Nov 19, 2024 · 5 comments
Labels

Comments

@huanlugwu
Copy link

huanlugwu commented Nov 19, 2024

What happened?

                      Planned arm      with any critical or major deviations  
                     TRT A    TRT B          TRT A                TRT B       
——————————————————————————————————————————————————————————————————————————————
ALL                   252      251          5 (2.0)              2 (0.8)      
Argentina              19       17          5 (26.3)             2 (11.8)      
  000001                5        2          5 (100)              2 (100)      

hello guys, I am working on this demanding table to count number of patients:

  • in columns: with and without flag
  • in rows: by country and site with different fomats, AND with different denominators in row within same ARM.

can anyone shine a light on this? thx!

@huanlugwu huanlugwu added the bug Something isn't working label Nov 19, 2024
@Melkiades
Copy link
Contributor

Hi @huanlugwu, could you provide a reproducible example? Avoid screenshots as they are not easily parsable (use {reprex} ;)

@huanlugwu
Copy link
Author

huanlugwu commented Nov 20, 2024

Thank you @Melkiades , so I made up these codes:

library(dplyr)
library(rtables)
library(forcats)

adsl_1 <- ex_adsl %>%
  select(USUBJID, ARM, ARMCD, BMEASIFL, COUNTRY, SITEID) %>%
  dplyr::mutate(
    COUNTRY = as.factor(COUNTRY),
    ARM = as.factor(ARM),
    LAYER1 = "Group 1",
    LAYER1 = factor(LAYER1)
  ) %>%
  dplyr::mutate(ARM = forcats::fct_reorder(ARM, rank(ARMCD))) %>%
  arrange(SITEID) %>%
  dplyr::mutate(SITEID = forcats::fct_inorder(SITEID))    

site_in_use <- adsl_1 %>%
  select(COUNTRY, SITEID) %>%
  distinct() %>%
  arrange(COUNTRY, SITEID) %>%
  mutate(COUNTRY=as.character(COUNTRY),
         SITEID=as.character(SITEID))

adsl_2 <- adsl_1 %>%
  filter(BMEASIFL == "Y") %>%
  dplyr::mutate(
    # LAYER1 to display the first row in header
    LAYER1 = "Group 2",
    LAYER1 = factor(LAYER1)
  )

cfun_cntrysite <- function(df, labelstr, .N_col, ...) {
  in_rows(
    rcell(nrow(df), format = "xx"),
    .labels = labelstr
  )
}

tbl_recipe <- basic_table() %>%
  rtables::split_cols_by(var = "LAYER1") %>%
  rtables::split_cols_by(var = "ARM") %>%
  rtables::summarize_row_groups(cfun = cfun_cntrysite) %>%
  rtables::split_rows_by("COUNTRY", split_fun = trim_levels_to_map(map = site_in_use)) %>%
  rtables::summarize_row_groups() %>%
  rtables::split_rows_by("SITEID", split_fun = trim_levels_to_map(map = site_in_use)) %>%
  rtables::summarize_row_groups()

tbl1 <- tbl_recipe %>%
  rtables::build_table(adsl_1)

tbl_recipe2 <- basic_table() %>%
  rtables::split_cols_by(var = "LAYER1") %>%
  rtables::split_cols_by(var = "ARM")%>%
  rtables::summarize_row_groups(cfun = cfun_cntrysite) %>%
  rtables::split_rows_by("COUNTRY", split_fun = trim_levels_to_map(map = site_in_use)) %>%
  rtables::summarize_row_groups() %>%
  rtables::split_rows_by("SITEID", split_fun = trim_levels_to_map(map = site_in_use)) %>%
  rtables::summarize_row_groups() 

tbl2 <- tbl_recipe2 %>%
  rtables::build_table(adsl_2)

t <- cbind_rtables(tbl1, tbl2)
t

Then you should get:

> t
                             Group 1                                    Group 2                 
             A: Drug X    B: Placebo   C: Combination   A: Drug X    B: Placebo   C: Combination
————————————————————————————————————————————————————————————————————————————————————————————————
                134          134            132             68           73             62      
  CHN        74 (55.2%)   81 (60.4%)     64 (48.5%)     35 (51.5%)   44 (60.3%)     25 (40.3%)  
    CHN-1    21 (15.7%)   20 (14.9%)     16 (12.1%)     10 (14.7%)   14 (19.2%)     9 (14.5%)   

What I need is that, in a row, with same ARM, the fraction of group2 is counted by using the number in group1 as denominator. I updated the example in my original post above.

@shajoezhu shajoezhu removed the bug Something isn't working label Nov 20, 2024
@shajoezhu
Copy link
Contributor

Hi @huanlugwu , many thanks for sharing this interesting use case.

I agree, your approach of creating two tables is correct, and it is to cbind them.

The ask was to compute the fraction following 35 by 35/74 rather than 35/68.

Can you please post this question at StackOverflow with the tag nest-tern, and we will follow up there. closing this issue for now

@huanlugwu
Copy link
Author

consider it done: https://stackoverflow.com/staging-ground/79206010

@shajoezhu
Copy link
Contributor

hi @huanlugwu , the link is broken. Thanks

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

No branches or pull requests

3 participants