简体   繁体   English

在 R 表 1 package 中使用自定义 function

[英]Use custom function in R table1 package

I am trying to build data summary tables in R using the table1 package.我正在尝试使用 table1 package 在 R 中构建数据汇总表。 Does anyone know if it is possible to specify custom functions to render instead of the stats.default functions?有谁知道是否可以指定要呈现的自定义函数而不是stats.default函数? I want to use the function geoSD and geoMean from the EnvStats package.我想使用来自 EnvStats package 的 function geoSD 和 geoMean。 Any ideas?有任何想法吗?

You can pass in your own custom rendering functions.您可以传入自己的自定义渲染函数。 The function should produce a named character vector where the name of the first element will be replaced by the name of the variable. function 应该生成一个命名字符向量,其中第一个元素的名称将被变量的名称替换。 If you wish to avoid that behavior, make the first element an empty string.如果您希望避免这种行为,请将第一个元素设为空字符串。

library(table1)

render.continuous.custom <- function(x, ...) {
  attr(x, "label") <- NULL # strip labels because geo.+() will choke if present
  c(
    "", 
    "geoMean" = format(round(EnvStats::geoMean(x), 3), nsmall = 3), 
    "geoSD"   = format(round(EnvStats::geoSD(x),   3), nsmall = 3)
  )
}

table1(~ mpg | factor(cyl), mtcars, render.continuous = render.continuous.custom)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM