如何在Disposition表中设置特定列留空,仅保留总计列数值?
制作Disposition表的技术问题及解决方法
需求背景
制作入组情况计算用的Disposition表,要求保留total number列(示例中为"Overall"列)的数值,指定的treatment列需保留列结构但内容为空,最终需通过rbind()函数与其他表部分合并。
待解决问题
- 如何使指定列的内容为空?
- 能否指定两列内容完全为空,仅保留
total number列的数值?
现有代码
lyt <- basic_table(show_colcounts = FALSE ) %>% split_cols_by( "TRT01P", split_fun = remove_split_levels(c("Not Assigned", "Screen Failure")) ) %>% add_overall_col(label="Overall")%>% count_patients_with_flags( var = "USUBJID", flag_variables = "ENRLFL", .stats = "count", ) %>% count_patients_with_flags( var = "USUBJID", flag_variables = "NERFL", .stats = "count", .indent_mods = 1 ) %>% analyze_vars( "ENTERCOM_f", .stats = "count", denom = "N_col", show_labels = "hidden", .indent_mods = c(count = 2L) ) lyt2 <- basic_table(show_colcounts = FALSE ) %>% split_cols_by( "TRT01P", split_fun = remove_split_levels(c("Not Assigned", "Screen Failure")) ) %>% add_overall_col(label="Overall")%>% count_patients_with_flags( var = "USUBJID", flag_variables = "RANDFL_", .stats = "count", )%>% # count_patients_with_flags( # var = "USUBJID", # flag_variables = "NOTRTFL", # # .stats = "count", # .indent_mods = c(count = 2L), # )%>% count_values("NOTRTFL", values = "Y", .labels = c(count_fraction = "Not treated"), .indent_mods = c(count_fraction = 2L) )%>% count_values("TRTFL", values = "Y", .labels = c(count_fraction = "Treated, i.e. Randomised Treatment"), .indent_mods = c(count_fraction = 2L) )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "OVRTFL", .stats = "count", .indent_mods = 3 )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "OVRCOM", .stats = "count", .indent_mods = 4 )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "OVRONGO", .stats = "count", .indent_mods = 4 )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "OVRDIS", .stats = "count", .indent_mods = 4 )%>% analyze_vars( "OVRDIS_f", .stats = "count_fraction", denom = "N_col", show_labels = "hidden", .indent_mods = c(count_fraction = 5L) )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "RANDTFL", .stats = "count", .indent_mods = 3 )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "RANDCOM", .stats = "count", .indent_mods = 4 )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "RANDONGO", .stats = "count", .indent_mods = 4 )%>% count_patients_with_flags( var = "USUBJID", flag_variables = "RANDDIS", .stats = "count", .indent_mods = 4 )%>% analyze_vars( "RANDDIS_f", .stats = "count_fraction", denom = "N_col", show_labels = "hidden", .indent_mods = c(count_fraction = 5L) ) result1 <- build_table(lyt = lyt, df = adsl) result1 result2 <- build_table(lyt = lyt2, df = adsl2) result2 col_info(result2) <- col_info(result1) result <- rbind(result1, result2)
解决方法
1. 清空指定列内容
构建完表格后,直接通过列索引或列名定位目标列,将内容替换为空字符串(或NA)即可:
# 方式1:通过列索引定位(假设treatment列是第2、3列,根据实际结构调整) result1[, 2:3] <- "" # 方式2:通过列名精准匹配(假设"Overall"是total number列,其余为treatment列) trt_cols <- setdiff(colnames(result1), "Overall") result1[, trt_cols] <- ""
2. 仅保留total number列数值,清空其他指定列
逻辑和上述一致,先确定total number对应的列(示例中为"Overall"),然后将所有需要清空的列内容置空:
# 保留Overall列,清空所有treatment列 trt_cols <- setdiff(colnames(result2), "Overall") result2[, trt_cols] <- ""
处理后的表格列结构保持不变,仅指定列内容为空,可直接通过rbind()与其他表格合并。
内容的提问来源于stack exchange,提问作者xiecheng gu
相关产品推荐
相关产品推荐

