How can I subscript names in a table from kable()?

AJMA picture AJMA · Apr 10, 2017 · Viewed 7.2k times · Source

Given a data.frame A, how can I use subscripted rows and columns names? Eventually I want produce a table through kable() in rmarkdown (output: word document).

A <- data.frame(round(replicate(3, runif(2)),2))
rownames(A) <- c("Hola123", "Hola234")
A

          X1   X2   X3
Hola123 0.47 0.55 0.66
Hola234 0.89 0.45 0.20

How could I make all numbers from row and column names subscripted when creating a table through kable(A)?

I have tried:

rownames(A) <- c(expression(Hola["123"]), expression(Hola["234"]))
names(A) <- c(expression(X["1"]), expression(X["2"]), expression(X["3"]))

But it does not appears subscripted when creating the table through kable() in the .rmd file.

Answer

S&#233;bastien Rochette picture Sébastien Rochette · Apr 10, 2017

To add subscripts in a rmarkdown document, you can embed your text between two tilde: text~sub~. When using function kable, any text in the table is recognized as markdown syntax. So that your rmarkdown code should be:

```{r}
A <- data.frame(round(replicate(3, runif(2)),2))
rownames(A) <- c("Hola~123~", "Hola~234~")
names(A) <- c("X~1~", "X~2~", "X~3~")
knitr::kable(A)
```