有六個回歸模型。我使用了更寬的樞軸,但很難閱讀。我可以使用兩級標題嗎?
- 第一級 - 回歸模型
- 第二級 - 估計,tstat
library(dplyr)
regression <- c(rep("A", 3), rep("B", 3), rep("C", 3), rep("D", 3), rep("E", 3), rep("F", 3))
attribute <- rep(c("b0", "b1", "b2"), 6)
estimate <- round(runif(n = 18, min = 0, max = 10), 2)
tstat <- round(runif(n = 18, min = 0, max = 10), 2)
# tibble
tbl <- tibble(regression, attribute, estimate, tstat)
# pivot wider
tbl <- tbl %>%
pivot_wider(names_from = regression,
values_from = c("estimate", "tstat"))
uj5u.com熱心網友回復:
一種選擇是separate_header從ftExtra
library(ftExtra)
library(dplyr)
library(tidyr)
library(stringr)
tbl %>%
pivot_wider(names_from = regression,
values_from = c("estimate", "tstat")) %>%
rename_with(~ str_replace(., "(.*)_(.*)", "\\2_\\1"), -1) %>%
as_flextable() %>%
separate_header()
-輸出

或者可以使用 span_header
library(flextable)
tbl %>%
pivot_wider(names_from = regression,
values_from = c("estimate", "tstat")) %>%
rename_with(~ str_replace(., "(.*)_(.*)", "\\2_\\1"), -1) %>%
select(attribute, order(str_remove(names(.)[-1], "_.*")) 1) %>%
as_flextable() %>%
span_header() %>%
align(align = "center", part = "all")
-輸出

如果我們需要將某些列加粗,
tbl %>%
pivot_wider(names_from = regression,
values_from = c("estimate", "tstat")) %>%
rename_with(~ str_replace(., "(.*)_(.*)", "\\2_\\1"), -1) %>%
mutate(across(ends_with('tstat'), ~sprintf('**%.2f**', .))) %>%
select(attribute, order(str_remove(names(.)[-1], "_.*")) 1) %>%
as_flextable() %>%
span_header() %>%
align(align = "center", part = "all") %>%
colformat_md()
-輸出

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343053.html
