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

如何在{gtsummary}中移除单个变量的big.mark?

如何仅移除单个变量的big.mark格式?

当然可以单独移除单个变量的big.mark,不用修改全局主题设置。针对出生年份这类不需要千位分隔符的变量,直接单独指定格式规则即可,以下是几种常见场景的实现方式:

场景1:用dplyr生成汇总统计时单独格式化

如果是用dplyr计算汇总后输出,直接对目标变量用format()函数指定big.mark = "":

library(dplyr)

# 示例数据
df <- tibble(birth_year = c(1988, 1990, 1992), value = c(12345, 67890, 13579))

# 汇总时单独格式化出生年份
summary_df <- df %>%
  summarise(
    avg_year = format(mean(birth_year), big.mark = ""),
    total_value = sum(value)  # 该变量保留全局big.mark格式
  )

场景2:ggplot2可视化中单独设置坐标轴格式

如果是ggplot的坐标轴显示出生年份,用scale_x_continuous()或scale_y_continuous()的labels参数自定义格式:

library(ggplot2)

ggplot(df, aes(x = birth_year, y = value)) +
  geom_col() +
  scale_x_continuous(labels = function(x) format(x, big.mark = ""))

场景3:表格工具(如gt、flextable)中单独设置列格式

以gt包为例,针对指定列关闭千位分隔符:

library(gt)

df %>%
  gt() %>%
  fmt_number(
    columns = birth_year,
    sep_mark = ""  # 移除千位分隔符
  )

核心思路就是针对单个变量/列单独应用格式规则,跳过全局的big.mark设置,这样既能保留其他变量的千位分隔符,又能让出生年份显示为无逗号的纯数字。

内容的提问来源于stack exchange,提问作者Doron Shiffer-Sebba

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 14:10:28