以成对列的行分母计算患者的百分比

问题描述 投票:0回答:1

我正在制作这张要求很高的表格来计算患者数量:

  • 在列中:有或没有标志
  • 行中:按国家和站点使用不同的格式,并且在同一 ARM 中行中使用不同的分母。

要重现结果,您可以尝试以下操作:

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

然后得到这个:

> 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%)   

但我需要的是,在同一行中,使用相同的ARM,使用group1中的数字作为分母来计算group2的分数。

理想情况下,我需要这个:

> 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 (47.3%)   44 (54.3%)     25 (39.1%)  
    CHN-1    21 (15.7%)   20 (14.9%)     16 (12.1%)     10 (47.6%)   14 (70.0%)     9 (56.3%)   

有人能解释一下吗?谢谢!

r nest nest-rtables nest-tern
1个回答
0
投票

可以通过创建自定义分析函数并在

summarize_row_groups()
调用中使用它来构建此表。自定义分析函数能够利用
.spl_context
参数,该参数提取有关当前表上下文的信息,包括先前的列数据。

创建此表:

library(rtables)

adsl <- ex_adsl %>%
  dplyr::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))) %>%
  dplyr::arrange(SITEID) %>%
  dplyr::mutate(SITEID = forcats::fct_inorder(SITEID))    

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

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

## custom analysis function
cfun_custom_denom <- function(df, labelstr, .N_col, .spl_context, ...) {
  denom <- if (.spl_context$cur_col_split_val[[1]][1] == "Group 2") {
    .spl_context[[paste0("Group 1.", .spl_context$cur_col_split_val[[1]][2])]] %>%
      tail(1) %>%
      unlist() %>%
      sum()
  } else {
    .N_col
  }
  rcell(c(nrow(df), nrow(df) / max(denom, 1)), format = "xx (xx.x%)", label = labelstr)
}

tbl_recipe <- basic_table() %>%
  split_cols_by("LAYER1") %>%
  split_cols_by("ARM") %>%
  summarize_row_groups(format = "xx") %>%
  split_rows_by("COUNTRY", split_fun = trim_levels_to_map(map = site_in_use)) %>%
  summarize_row_groups(cfun = cfun_custom_denom) %>%
  split_rows_by("SITEID") %>%
  summarize_row_groups(cfun = cfun_custom_denom)

t <- tbl_recipe %>% build_table(df)

head(t, 3)
#>                             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 (47.3%)   44 (54.3%)     25 (39.1%)  
#>     CHN-1   21 (15.7%)   20 (14.9%)     16 (12.1%)     10 (47.6%)   14 (70.0%)     9 (56.2%)

创建于 2024 年 11 月 21 日,使用 reprex v2.1.1

请注意,我在上面的示例中创建的自定义分析函数是专门针对该表的,对表结构的任何更改都可能需要对分析函数进行更改。

有关创建自定义分析函数的更多信息,请阅读以下 {rtables} 文章:

© www.soinside.com 2019 - 2024. All rights reserved.