Change row names in kable (RMarkdown)

70 views Asked by At

I have produced the following table with kable package, in RMarkdown.

Here's the dataframe:

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"))

And the 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;")

Is there any way I can leave the name of column 0 blank, and name of row seven as Demanda interna^1, where 1 is supposed to be superscript?

1

There are 1 answers

0
Peter On BEST ANSWER

Is this what you are looking for?

You can force a column header to be blank by using "" in the call to kable(col.names=...).

Use the maths environment $...$ with a bit of help from LaTeX \\text{} (note the double backslash to escape the single backslash) to retain part string element and add superscript at the end of the string.

We can also simplify the multiple calls to row_spec().

I've assumed you do not want to include row names.

I also think you are mixing pdf and html arguments. It is not clear what output you intend. I have assumed pdf due to the use of booktabs and latex_options. But bootstrap_options, I think, are for html output.

---
output: pdf_document
---

```{r packages, include=FALSE}

library(kableExtra)

```



```{r data, include=FALSE}

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"))


```


```{r}

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;")

```

Which results in:

enter image description here