Skip to content

[Question]: custom summarize in group_desc () to obtain total n under same subgroup #189

@haodafa2019

Description

@haodafa2019

Description

I tried to create table for frequency and descriptive summary for variable SEX and AGE.
For SEX, I can obtain different pct by define set_denoms_by() which is very user friendly.
But for AAGE, I can obtain non-missing n and missing by each subgroup (TRT01A, AAGEGR1), but I could not obtain the denominator by group (TRT01A) or (TRT01AN, ARACE), do you have suggestion?

Code snippet

demo <- tplyr_table(adsl, TRT01A, cols = AAGEGR1) %>% 
  set_pop_data(adsl) %>% 
  set_pop_treat_var(TRT01A) %>% 
  add_total_group(group_name = "99") %>%
  add_layer(
    group_count(SEX,by=ARACE) %>%
      add_total_row(f_str(""), count_missings=FALSE, sort_value=-999) %>%
      set_format_strings(f_str("xx (x.x)", n, pct))%>%
      set_missing_count(f_str("xx (x.x)", n, pct), Missing = NA, sort_value = 98) %>% 
      set_total_row_label("Sex, n (%)") %>% 
      set_denoms_by(TRT01A,AAGEGR1,ARACE)
   ) %>% 
  add_layer(
    group_desc(AAGE, by=ARACE) %>% 
      set_custom_summaries(
        blank_=as.numeric(NA),
        nmiss=sum(!is.na(.var)),
        nmiss_pct=sum(!is.na(.var))/n*100,
        missing = sum(is.na(.var)),
        missing_pct= sum(is.na(.var))/n*100
      ) %>% 
      set_format_strings(
        "Age (Years)"  = f_str("xx", blank_),
        "Total n"            = f_str("xx",n),
        "n (%)"       = f_str("xx (x.x)", nmiss, nmiss_pct),
        "Missing (%)" = f_str("xx (x.x)", missing, missing_pct),
        "Mean +/-SD" = f_str("xx.x +/-x.xx", mean,sd, empty='NA'),
        "Median"    = f_str("xx.x", median),
        "Q1; Q3"    = f_str("xx.x; xx.x", q1, q3, empty=c(.overall='NA')),
        "Min; Max"  = f_str("xx; xx", min, max)
      ) 
  )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions