You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R语言table1()函数实现供体与受体变量分组表格布局?

使用R的table1()函数实现变量分组行布局

问题描述

我正在尝试用R的table1()函数生成表格,想要模仿目标格式(Desired layout of Table1),但当前生成的表格(Generated Table1 from table1() function)无法实现供体(Donor)和受体(Recipient)变量的分组行布局(对应图中标注g1、g2的行)。我试过用键分离变量、用strata参数按行分层都没成功,当前代码如下:

library(table1)

label(t1data$`Recipient Age`) <- "Age, y"
label(t1data$`Recipient Sex`) <- "Sex"
label(t1data$`Recipient Race`) <- "Race"
label(t1data$`Recipient Ethnicity`) <- "Ethnicity"
 
label(t1data$`Donor Age`) <- "Age, y"
label(t1data$`Donor Gender`) <- "Sex"
label(t1data$`Donor Race`) <- "Race"
label(t1data$`Donor Ethnicity`) <- "Ethnicity"

table1( ~ `Recipient Age` + `Recipient Sex` + `Recipient Ethnicity` + `Recipient Race` +
          `Donor Age` + `Donor Gender` + `Donor Ethnicity` + `Donor Race` | Status, data = t1data, overall = FALSE, rowlabelhead = "Demographics")

我想知道是需要先预处理数据,还是table1()本身有参数可以实现这个需求?

解决方法

不需要额外预处理数据,table1()函数本身支持通过公式分组或groups参数实现行分组布局,具体有两种可行方式:

方式1:公式内括号分组变量

直接在table1()的公式中,将受体、供体变量分别放在括号内,每组会自动生成分组标题行,还可通过groupLabels自定义标题:

library(table1)

# 保留原变量标签设置
label(t1data$`Recipient Age`) <- "Age, y"
label(t1data$`Recipient Sex`) <- "Sex"
label(t1data$`Recipient Race`) <- "Race"
label(t1data$`Recipient Ethnicity`) <- "Ethnicity"
 
label(t1data$`Donor Age`) <- "Age, y"
label(t1data$`Donor Gender`) <- "Sex"
label(t1data$`Donor Race`) <- "Race"
label(t1data$`Donor Ethnicity`) <- "Ethnicity"

# 修改公式,按受体/供体分组变量
table1( ~ (Recipient Age + Recipient Sex + Recipient Ethnicity + Recipient Race) + 
          (Donor Age + Donor Gender + Donor Ethnicity + Donor Race) | Status, 
        data = t1data, 
        overall = FALSE, 
        rowlabelhead = "Demographics",
        groupLabels = c("Recipient", "Donor")) # 自定义分组标题

方式2:用groups参数指定分组

如果变量命名有统一前缀(如Recipient/Donor),可先定义分组映射向量,再传入groups参数:

library(table1)

# 保留原变量标签设置
label(t1data$`Recipient Age`) <- "Age, y"
label(t1data$`Recipient Sex`) <- "Sex"
label(t1data$`Recipient Race`) <- "Race"
label(t1data$`Recipient Ethnicity`) <- "Ethnicity"
 
label(t1data$`Donor Age`) <- "Age, y"
label(t1data$`Donor Gender`) <- "Sex"
label(t1data$`Donor Race`) <- "Race"
label(t1data$`Donor Ethnicity`) <- "Ethnicity"

# 定义变量分组映射
var_groups <- ifelse(grepl("Recipient", names(t1data)), "Recipient", 
                     ifelse(grepl("Donor", names(t1data)), "Donor", NA))

table1( ~ `Recipient Age` + `Recipient Sex` + `Recipient Ethnicity` + `Recipient Race` +
          `Donor Age` + `Donor Gender` + `Donor Ethnicity` + `Donor Race` | Status, 
        data = t1data, 
        overall = FALSE, 
        rowlabelhead = "Demographics",
        groups = var_groups)

补充说明

  • 两种方式都会生成带有Recipient/Donor分组标题的行布局,匹配你需要的g1、g2行效果;
  • 若要调整分组标题样式,可搭配render.grouplabel参数自定义(如加粗、调整字体等);
  • 全程无需转换数据格式,直接使用原宽格式数据即可。

内容的提问来源于stack exchange,提问作者user22770348

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 03:17:06