更改可更改的行名称(RMarkdown)

vsdwdz23  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(62)

我用RMarkdown中的kable包生成了下面的表。
下面是一个例子:

structure(list(...1 = c("Catalunya", "PIB", "Demanda interna1", 
"Demanda externa1", "Espanya", "PIB", "Demanda interna1", "Demanda externa1", 
"Zona euro", "PIB", "Demanda interna1", "Demanda externa1", "PIB d'Alemanya", 
"PIB de França", "PIB d'Itàlia", "Internacional", "PIB de la UE-27", 
"PIB dels EUA", "PIB del Japó"), `2020` = c(NA, -11.9, -7.4, 
-4.5, NA, -11.2, -9, -2.2, NA, -6.1, -5.57, -0.53, -3.8, -7.5, 
-9, NA, -5.6, -2.2, -4.3), `2021` = c(NA, 6.2, 3.8, 2.4, NA, 
6.4, 5.9, 0.5, NA, 5.6, 4.24, 1.36, 3.2, 6.4, 8.3, NA, 5.7, 5.8, 
2.1), `2022` = c(NA, 5.5, 2.7, 2.8, NA, 5.5, 5.12183164381576, 
0.378168356184236, NA, 3.6, 3.76, -0.16, 1.8, 2.5, 3.7, NA, 3.5, 
1.9, 1.1), `1r 2023` = c(NA, 3.2, 1.1, 2.1, NA, 4.1, 1.3, 2.8, 
NA, 1.2, 0.63, 0.57, -0.2, 1, 2.1, NA, 1.1, 1.71772942152937, 
2), `2n 2023` = c(NA, 2.1, 1.2, 0.9, NA, 2, 2.2, -0.2, NA, 0.5, 
0.46, 0.04, 0.1, 1.1, 0.3, NA, 0.4, 2.38237207285343, 1.7), `3r 2023` = c(NA, 
"2.1", "-", "-", NA, "1.8", "1.6", "0.2", NA, "0.1", "-", "-", 
"-0.4", "0.7", "0", NA, "0.1", "3", "1.2")), row.names = c("1", 
"2", "aaaa", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19"), class = c("tbl_df", 
"tbl", "data.frame"))

字符串
table:

options(knitr.kable.NA = "")

kable(p3_5,
      align = "lrrrrrr",
      booktabs=TRUE,
      digits=1,
      format.args = list(big.mark="."),
      linesep = "" ,
      caption="Producte interior brut (\\% de variació interanual, llevat dels casos indicats)", escape   = FALSE) %>%
  add_header_above(c("", "Any" = 3, "Trimestre" = 3)) %>% 
  kable_styling(latex_options=c("HOLD_position","scale_down"), bootstrap_options = c("condensed"), position="center") %>% 
  column_spec(1, width="4cm") %>%
  column_spec(2:7, width="2cm") %>%
  row_spec(0, bold = T) %>%
  row_spec(1, background="lightgray") %>%
  row_spec(5, background="lightgray") %>%
  row_spec(9, background="lightgray") %>%
  row_spec(16, background="lightgray") %>%
  row_spec(19, extra_css = "border-bottom: 1px solid;")


有没有办法让column 0的名字空着,而第七行的名字是Demanda interna^1,其中1应该是上标?

klh5stk1

klh5stk11#

这是你要找的吗?
通过在对kable(col.names=...)的调用中使用"",可以强制列标题为空。
在LaTeX \\text{}的帮助下,使用数学环境$...$(注意双反斜杠是为了转义单反斜杠)来保留部分字符串元素,并在字符串的末尾添加上标。
我们还可以简化对row_spec()的多次调用。
我假设您不想包含行名称。
我也认为你是混合pdf和html参数.这是不清楚什么输出你打算.我已经假定pdf由于使用booktabslatex_options .但bootstrap_options,我认为,是html输出.

---
output: pdf_document
---

```{r packages, include=FALSE}

library(kableExtra)

p3_5 <- structure(list(...1 = c("Catalunya", "PIB", "Demanda interna1", 
"Demanda externa1", "Espanya", "PIB", "Demanda interna1", "Demanda externa1", 
"Zona euro", "PIB", "Demanda interna1", "Demanda externa1", "PIB d'Alemanya", 
"PIB de França", "PIB d'Itàlia", "Internacional", "PIB de la UE-27", 
"PIB dels EUA", "PIB del Japó"), `2020` = c(NA, -11.9, -7.4, 
-4.5, NA, -11.2, -9, -2.2, NA, -6.1, -5.57, -0.53, -3.8, -7.5, 
-9, NA, -5.6, -2.2, -4.3), `2021` = c(NA, 6.2, 3.8, 2.4, NA, 
6.4, 5.9, 0.5, NA, 5.6, 4.24, 1.36, 3.2, 6.4, 8.3, NA, 5.7, 5.8, 
2.1), `2022` = c(NA, 5.5, 2.7, 2.8, NA, 5.5, 5.12183164381576, 
0.378168356184236, NA, 3.6, 3.76, -0.16, 1.8, 2.5, 3.7, NA, 3.5, 
1.9, 1.1), `1r 2023` = c(NA, 3.2, 1.1, 2.1, NA, 4.1, 1.3, 2.8, 
NA, 1.2, 0.63, 0.57, -0.2, 1, 2.1, NA, 1.1, 1.71772942152937, 
2), `2n 2023` = c(NA, 2.1, 1.2, 0.9, NA, 2, 2.2, -0.2, NA, 0.5, 
0.46, 0.04, 0.1, 1.1, 0.3, NA, 0.4, 2.38237207285343, 1.7), `3r 2023` = c(NA, 
"2.1", "-", "-", NA, "1.8", "1.6", "0.2", NA, "0.1", "-", "-", 
"-0.4", "0.7", "0", NA, "0.1", "3", "1.2")), row.names = c("1", 
"2", "aaaa", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19"), class = c("tbl_df", 
"tbl", "data.frame"))

options(knitr.kable.NA = "")

p3_5[7,1] <- "$\\text{Demanda interna}^1$"

kable(p3_5,
      align = "lrrrrrr",
      booktabs=TRUE,
      col.names = c("", names(p3_5[-1])),
      row.names = FALSE,
      digits=1,
      format.args = list(big.mark="."),
      linesep = "" ,
      caption="Producte interior brut (\\% de variació interanual, llevat dels casos indicats)", escape   = FALSE) |>
  add_header_above(c("", "Any" = 3, "Trimestre" = 3)) |> 
  kable_styling(latex_options=c("HOLD_position","scale_down"), bootstrap_options = c("condensed"), position="center") |> 
  column_spec(1, width="4cm") |>
  column_spec(2:7, width="2cm") |>
  row_spec(c(1, 5, 9, 16), background="lightgray") |>
  row_spec(19, extra_css = "border-bottom: 1px solid;")
字符串
其结果是:

![](https://i.stack.imgur.com/XDuXV.png)
的数据

相关问题