【问题标题】:How to add custom descriptive statistics measures in a summary statistics table created using the table1 package如何在使用 table1 包创建的汇总统计表中添加自定义描述性统计度量
【发布时间】:2022-01-09 11:40:31
【问题描述】:

我正在使用 table1 包和下面的代码创建一个表格,其中包含位于闭合单位区间内的三个变量(即 ITE_tech、tech 和 IMR_tech)的区域特定描述性统计度量。

library(table1)
table1::label(results19$ITE_tech) <- "TE"
table1::label(results19$tech) <- "TE_k"
table1::label(results19$IMR_tech) <- "MTR"
table1::table1(~ITE_tech + tech + IMR_tech | REGION, data = results19)

如何在结果表中添加另一行,显示每个前面提到的变量取值为 1 的次数?我尝试使用下面的代码执行此操作,但此代码仅将 sum 函数应用于第一个变量,并从最终表中排除默认的描述性统计度量(即均值、标准差、最小值、最大值)。

render.continuous.custom <- function(x, ...) {
  attr(x, "label") <- NULL
  c(
    "", 
    "Eff. Obs." = sum(results19$tech==1) 
  )
}

table1::table1(~ITE_tech + tech + IMR_tech | REGION, data = results19, render.continuous = render.continuous.custom)

提前感谢您的帮助。

【问题讨论】:

    标签: r html-table customization summarytools


    【解决方案1】:

    下面是定义渲染函数的方法:

    render.continuous.custom <- function(x, ...) {
        y <- render.default(x, ...)
        c(y, "Eff. Obs."=sum(x==1))
    }
    

    (注意:一个可重现的例子会很有帮助)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 2022-08-21
      • 1970-01-01
      • 2018-10-30
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多