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

如何在gtsummary输出表中将数字末位四舍五入至0或5?

实现gtsummary表格数字末尾取整为0或5的方法

需求:将gtsummary生成的汇总表中所有数字的最后一位四舍五入为0或5。

用户提供的原始代码:

library(tidyverse)
library(gtsummary)

data %>% 
  tbl_summary(
    by = hba1c,
    include = -id
  ) 

数据结构:

structure(list(id = 1:100, age = c("a", "a", "b", "a", "b", "b", 
"b", "c", "c", "c", "a", "a", "b", "a", "b", "b", "b", "c", "c", 
"c", "a", "a", "b", "a", "b", "b", "b", "c", "c", "c", "a", "a", 
"b", "a", "b", "b", "b", "c", "c", "c", "a", "a", "b", "a", "b", 
"b", "b", "c", "c", "c", "a", "a", "b", "a", "b", "b", "b", "c", 
"c", "c", "a", "a", "b", "a", "b", "b", "b", "c", "c", "c", "a", 
"a", "b", "a", "b", "b", "b", "c", "c", "c", "a", "a", "b", "a", 
"b", "b", "b", "c", "c", "c", "a", "a", "b", "a", "b", "b", "b", 
"c", "c", "c"), sex = c("M", "F", "M", "M", "M", "F", "F", "F", 
"F", "M", "F", "M", "M", "M", "F", "F", "F", "F", "M", "F", "M", 
"M", "M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", "F", 
"F", "F", "M", "F", "M", "M", "M", "F", "F", "F", "F", "M", "F", 
"M", "M", "M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", 
"F", "F", "F", "M", "F", "M", "M", "M", "F", "F", "F", "F", "M", 
"M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", "F", "F", 
"F", "M", "M", "F", "F", "F", "F", "M", "F", "M", "M", "M", "F", 
"F"), country = c("eng", "eng", "eng", "eng", "wale", "wale", 
"wale", "scot", "scot", "scot", "eng", "eng", "eng", "eng", "wale", 
"wale", "wale", "scot", "scot", "scot", "eng", "eng", "eng", 
"eng", "wale", "wale", "wale", "scot", "scot", "scot", "eng", 
"eng", "eng", "eng", "wale", "wale", "wale", "scot", "scot", 
"scot", "eng", "eng", "eng", "eng", "wale", "wale", "wale", "scot", 
"scot", "scot", "eng", "eng", "eng", "eng", "wale", "wale", "wale", 
"scot", "scot", "scot", "eng", "eng", "eng", "eng", "wale", "wale", 
"wale", "scot", "scot", "scot", "eng", "eng", "eng", "eng", "wale", 
"wale", "wale", "scot", "scot", "scot", "eng", "eng", "eng", 
"eng", "wale", "wale", "wale", "scot", "scot", "scot", "eng", 
"eng", "eng", "eng", "wale", "wale", "wale", "scot", "scot", 
"scot"), edu = c("x", "x", "x", "y", "y", "y", "z", "z", "z", 
"z", "x", "x", "x", "y", "y", "y", "z", "z", "z", "z", "x", "x", 
"x", "y", "y", "y", "z", "z", "z", "z", "x", "x", "x", "y", "y", 
"y", "z", "z", "z", "z", "x", "x", "x", "y", "y", "y", "z", "z", 
"z", "z", "x", "x", "x", "y", "y", "y", "z", "z", "z", "z", "x", 
"x", "x", "y", "y", "y", "z", "z", "z", "z", "x", "x", "x", "y", 
"y", "y", "z", "z", "z", "z", "x", "x", "x", "y", "y", "y", "z", 
"z", "z", "z", "x", "x", "x", "y", "y", "y", "z", "z", "z", "z"
), hba1c = c(1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, 1L, 1L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA, NA, NA, NA, NA, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 
NA, NA, NA, NA, NA, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-100L))

解决方案

通过自定义格式化函数,结合gtsummary的modify_fmt_fun()方法即可实现需求:

  1. 定义数字末尾取整为0或5的函数:
round_to_05 <- function(x) {
  if (is.na(x)) return(x)
  # 核心逻辑:除以5后四舍五入,再乘以5
  rounded <- round(x / 5) * 5
  # 输出为简洁格式,如需保留小数可调整sprintf参数
  sprintf("%g", rounded)
}
  1. 在汇总表生成后应用该函数:
library(tidyverse)
library(gtsummary)

# 加载数据(将上述structure赋值给data)
data <- structure(...) # 替换为完整数据结构代码

data %>% 
  tbl_summary(
    by = hba1c,
    include = -id
  ) %>%
  # 对所有统计列应用自定义取整函数
  modify_fmt_fun(
    columns = all_stat_cols(),
    fmt_fun = round_to_05
  )

说明

  • round_to_05函数通过round(x/5)*5实现末尾取整,例如12→10、13→15、7→5、2→0。
  • 若需保留小数位(如处理百分比),可修改sprintf格式,比如sprintf("%.1f", rounded)保留一位小数。
  • 若仅需对特定列应用,可替换all_stat_cols()为具体列名,比如columns = c("stat_0", "stat_1")。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 18:42:02