如你所知,R对列的操作非常好。然而,行可能很困难。另一方面,Excel行很好,尤其是小计。为了解决我的问题,我需要在R表中实现Excel样式。
重要的是小计和总计。表子组上方的小计和第一个子组上方的总计。是否可以从gt() reference中改编代码,将分类汇总放在组上方,而不是tab_row_group中的ALL显示标签,并格式化整个汇总行。本例全部遗漏。谢谢
library(gt)
countrypops |>
dplyr::filter(
country_code_2 %in% c("BR", "RU", "IN", "CN", "FR", "DE", "IT", "GB")
) |>
dplyr::filter(year %% 10 == 0) |>
dplyr::select(country_name, year, population) |>
tidyr::pivot_wider(names_from = year, values_from = population) |>
gt(rowname_col = "country_name") |>
tab_row_group(
label = md("*BRIC*"),
rows = c("Brazil", "Russian Federation", "India", "China"),
id = "bric"
) |>
tab_row_group(
label = md("*Big Four*"),
rows = c("France", "Germany", "Italy", "United Kingdom"),
id = "big4"
) |>
row_group_order(groups = c("bric", "big4")) |>
tab_spanner(columns = everything(), label = "Year") |>
fmt_number(n_sigfig = 3, suffixing = TRUE) |>
summary_rows(
fns = list(label = md("**ALL**"), id = "totals", fn = "sum"),
fmt = ~ fmt_number(., n_sigfig = 3, suffixing = TRUE),
side = "top"
) |>
tab_style(
locations = cells_summary(),
style = cell_fill(color = "lightblue" |> adjust_luminance(steps = +1))
)
1条答案
按热度按时间mrfwxfqh1#
我不确定你在找什么,但这很接近吗?