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

如何在R的gt包中让汇总函数排除指定行?

解决gt包汇总行排除细分行的问题

方法1:通过is_breakdown列自定义汇总函数

可以直接利用is_breakdown标记在汇总函数中筛选非细分行,实现程序化的正确求和。gt的汇总函数支持通过.data访问当前组的完整数据,结合dplyr::filter即可排除细分行:

library(gt)
library(tibble)
library(dplyr)

df = tribble(
  ~group, ~name, ~is_breakdown, ~num, ~percent,
  "A",    "percent_happy"              , FALSE, 50, .5,
  "A",    "percent_of_happy_very_happy", TRUE , 5 , .1,
  "A",    "percent_sad"                , FALSE, 50, .5,
  "B",    "percent_happy"              , FALSE, 50, .5,
  "B",    "percent_sad"                , FALSE, 50, .5,
  "B",    "percent_of_sad_very_sad"    , TRUE , 10, .2
)

gt(df, groupname_col = "group", rowname_col = "name") |>
  tab_stub_indent(rows = df$is_breakdown, indent = 4) |>
  # 组内汇总:仅计算非细分行的总和
  summary_rows(
    columns = c("num", "percent"),
    fns = list(
      label = "Sub-Total", id = "sub-total" ~ sum(filter(.data, !is_breakdown)$num),
      label = "Sub-Total", id = "sub-total" ~ sum(filter(.data, !is_breakdown)$percent)
    )
  ) |>
  # 总计行:仅计算非细分行的总和
  grand_summary_rows(
    columns = c("num", "percent"),
    fns = list(
      label = "Grand Total", id = "grand-total" ~ sum(filter(.data, !is_breakdown)$num),
      label = "Grand Total", id = "grand-total" ~ sum(filter(.data, !is_breakdown)$percent)
    )
  )

方法2:手动创建汇总行(灵活定制场景)

如果需要对汇总行的样式、位置有更多控制,可以先手动生成汇总数据,再合并到原数据集后构建表格:

library(gt)
library(tibble)
library(dplyr)

df = tribble(
  ~group, ~name, ~is_breakdown, ~num, ~percent,
  "A",    "percent_happy"              , FALSE, 50, .5,
  "A",    "percent_of_happy_very_happy", TRUE , 5 , .1,
  "A",    "percent_sad"                , FALSE, 50, .5,
  "B",    "percent_happy"              , FALSE, 50, .5,
  "B",    "percent_sad"                , FALSE, 50, .5,
  "B",    "percent_of_sad_very_sad"    , TRUE , 10, .2
)

# 生成组内汇总数据
group_totals = df |>
  filter(!is_breakdown) |>
  group_by(group) |>
  summarise(
    name = "Sub-Total",
    is_breakdown = NA,
    num = sum(num),
    percent = sum(percent),
    .groups = "drop"
  )

# 生成总计数据
grand_total = df |>
  filter(!is_breakdown) |>
  summarise(
    group = NA,
    name = "Grand Total",
    is_breakdown = NA,
    num = sum(num),
    percent = sum(percent)
  )

# 合并数据并排序,保证汇总行位置正确
combined_df = bind_rows(df, group_totals, grand_total) |>
  arrange(group, is_breakdown, factor(name, levels = c(unique(df$name), "Sub-Total", "Grand Total")))

# 构建带样式的gt表格
gt(combined_df, groupname_col = "group", rowname_col = "name") |>
  tab_stub_indent(rows = combined_df$is_breakdown, indent = 4) |>
  # 给汇总行添加加粗和灰色背景样式
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_stub(rows = name %in% c("Sub-Total", "Grand Total"))
  ) |>
  tab_style(
    style = cell_fill(color = "#f0f0f0"),
    locations = cells_body(rows = name %in% c("Sub-Total", "Grand Total"))
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 23:11:22