使用 function 获得的 flextable 函数的输出as_grouped_data()。
df = structure(list(variable = c("something", NA, NA, NA), var = c(NA, "(Intercept)", "mutate1", "variable"), estimate = c(NA, 3.64770410229416, -0.230158472032055, -0.000692974348090823), std.error = c(NA, 0.88, 0.0831, 0.9315), statistic = c(NA, 0.1933, -0.5458, -0.613), df = c(NA, 67.03, 53.27, 58.285), p.value = c(NA, "<0.001", "0.80", "0.87")), row.names = c(NA, 4L), class = c("grouped_data", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
这给出了类似的东西:
variable var estimate std.error statistic df p.value
1 something <NA> NA NA NA NA <NA>
2 <NA> (Intercept) 3.6477041023 0.8800 0.1933 67.030 <0.001
3 <NA> mutate1 -0.2301584720 0.0831 -0.5458 53.270 0.80
4 <NA> variable -0.0006929743 0.9315 -0.6130 58.285 0.87
Run Code Online (Sandbox Code Playgroud)
由于我正在使用 flextable,因此可能得到的是:
df %>% flextable %>% colformat_double(j = c('estimate','std.error', 'statistic','df'))
Run Code Online (Sandbox Code Playgroud)
我知道它可以被视为一个数据帧,我想在将其转换为表格之前进行修改,以便我最终可以得到这个结果,可能使用 dplyr
variable
something NA NA NA NA NA
var estimate std.error statistic df p.value
(Intercept) 3.6477041023 0.8800 0.1933 67.030 <0.001
mutate1 -0.2301584720 0.0831 -0.5458 53.270 0.80
variable -0.0006929743 0.9315 -0.6130 58.285 0.87
Run Code Online (Sandbox Code Playgroud)
因此我想要得到的决赛桌是
variable NA. NA NA NA NA
1 something <NA> <NA> <NA> <NA> <NA>
2 var estimate std.error statistic df p.value
3 (Intercept) 3.6 0.9 0.2 67 <0.001
4 mutate -0.2 0.1 -0.5 53.3 0.8
5 variable -0.2 0.9 0.6 53.3 0.87
Run Code Online (Sandbox Code Playgroud)
作为弹性表显示返回
只是为了获得这个想法,包含带有 X 点和编号 NA 的列,而这些列应该为空白。是否可以按照我的意愿用 dplyr 调整数据框。
它也没有回答 dplyr 问题,我认为它可以帮助你做你想做的事。它满足了在弹性表中的模型摘要上方添加行的需求:
library(flextable)
options(show.signif.stars = FALSE)
dat <- attitude
dat$high.rating <- (dat$rating > 70)
probit.model <- glm(high.rating ~ learning + critical +
advance, data = dat, family = binomial(link = "probit"))
ft <- as_flextable(probit.model) |>
delete_part(part = "footer") |>
add_header_lines(values = c("variable", "something")) |>
theme_booktabs()
ft
Run Code Online (Sandbox Code Playgroud)
更完整的演示展示了如何在标题或正文中添加行:
ft <- as_flextable(probit.model) |>
delete_part(part = "footer") |>
add_header_row(values = c("on 2 columns", "on 3 columns"), colwidths = c(2, 3)) |>
add_header_lines(values = c("variable", "something")) |>
add_body_row(values = c("blah blah", "bleeh"), colwidths = c(3, 2), top = TRUE) |>
add_body_row(values = c("bleeh bleeh", "blah"), colwidths = c(3, 2), top = FALSE) |>
theme_vanilla() |>
color(i = ~ p.value > 0.05, color = "gray") |>
align(part = "header", i = 1:3, align = "left")
ft
Run Code Online (Sandbox Code Playgroud)