重命名 gt() 表中的行值

Renaming row values in gt() tables

我试图从 gt() table 中删除字符串,但没有成功。 在我的 table 中,我想要多个列 names/titles 涵盖名为“第 0 天”、“第 4 天”、“第 10 天”、“第 17 天”的两列以及名为“mean”和“se”的子列.看数据可能更容易理解。

我现在想要的是从子栏中删除“第 0 天”(或例如“第 4 天”)。 有什么建议吗?

我的代码:

library(gt)

gt(mean_se)%>%
  tab_spanner(label = "Day 0", columns = matches("0")) %>%
  tab_spanner(label = "Day 4", columns = matches("4")) %>%
  tab_spanner(label = "Day 10", columns = matches("10")) %>%
  tab_spanner(label = "Day 17", columns = matches("17")) %>%
  tab_header(
    title = md("Biogenic volatile organic compound emissions"))
  
  # str_remove(mean_se, "day 0")
  # rename("mean"=="mean day 0")

这是数据:

mean_se <- structure(list(mass = c("i.x33.031", "i.x35.034", "i.x36.017", 
"i.x39.959", "i.x40.023"), `mean day 0` = c("241.82", "0.36", 
"1.78", "0.2", "1.82"), `se day 0` = c("241.82", "0.36", "1.78", 
"0.2", "1.82"), `mean day 4` = c("32.94", "0.14", "0", "0", "1.74"
), `se day 4` = c("32.94", "0.14", "0", "0", "1.74"), `mean day 10` = c("266.28", 
"0.6", "0", "0", "1.58"), `se day 10` = c("266.28", "0.6", "0", 
"0", "1.58"), `mean day 17` = c("451.4", "0.48", "0", "0", "2.94"
), `se day 17` = c("451.4", "0.48", "0", "0", "2.94")), row.names = c(NA, 
-5L), class = "data.frame")

一种方法是 cols_label。此函数接受个人参数以重命名特定列,例如 "mean day 0" = "mean"。但是把所有这些都打出来需要做很多工作。相反,我们可以使用 .list = 并提供我们要重命名的所有列的命名列表。

我们可以重复 "mean""se" 适当的次数,并用列名命名该字符向量:

setNames(rep(c("mean","se"),(ncol(mean_se)-1)/2),names(mean_se)[-1])
# mean day 0    se day 0  mean day 4    se day 4 mean day 10   se day 10 mean day 17   se day 17 
#     "mean"        "se"      "mean"        "se"      "mean"        "se"      "mean"        "se" 

为了保持顺序正确,我们可以使用cols_move

gt(mean_se)%>%
  tab_spanner(label = "Day 0", columns = matches("0")) %>%
  tab_spanner(label = "Day 4", columns = matches("4")) %>%
  tab_spanner(label = "Day 10", columns = matches("10")) %>%
  tab_spanner(label = "Day 17", columns = matches("17")) %>%
  cols_label(.list = setNames(rep(c("mean","se"),(ncol(mean_se)-1)/2),
                              names(mean_se)[-1])) %>%
  cols_move(columns = names(mean_se)[-1], after = names(mean_se)[1]) %>%
  tab_header(
    title = md("Biogenic volatile organic compound emissions"))