Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create table in rmarkdown using greek letters in column headers?

I am trying to create a table in rmarkdown with kable and kableExtra and I want to put greek letters in the add_header_above function.

kable(a, format = "latex", booktabs = T, longtable = T) %>% 
kable_styling() %>%
add_header_above(c("n", "$\\alpha$", "Estimates for $\\alpha$" = 4, 
"Estimates for $\\beta$" = 4), align = "l", escape = F)

here

I tried using escape = F like here, but it seems it doesn't work for greek letters.

I want that style of table in my rmarkdown document with the greek letters in the column headers. Is there a way to do that with kable and kableExtra or even with another method?

My data:

a <- structure(list(c("5", "", "", "", ""), c(0.1, 0.25, 0.5, 1, 2
), MLE = c(0.0839, 0.2082, 0.4194, 0.8237, 1.6201), MME = c(0.0839, 
0.2082, 0.4194, 0.8234, 1.6147), UMLE = c(0.1048, 0.2602, 0.5242, 
1.0296, 2.0251), UMME = c(0.1048, 0.2602, 0.5242, 1.0293, 2.0183
), MLE = c(1, 1.0057, 1.0232, 1.0824, 1.2543), MME = c(1, 1.0057, 
1.0232, 1.0824, 1.2551), UMLE = c(0.9994, 1.0019, 1.0077, 1.0233, 
1.0456), UMME = c(0.9994, 1.0019, 1.0077, 1.0233, 1.0476)), .Names = c("", 
"", "MLE", "MME", "UMLE", "UMME", "MLE", "MME", "UMLE", "UMME"
), row.names = c(NA, 5L), class = "data.frame")
like image 470
falecomdino Avatar asked Apr 26 '18 14:04

falecomdino


People also ask

How do you type Greek letters in R markdown?

To make a Greek letter in R, You just use \ and then the name of the letter. If you want a subscript, like β1 , you use $\beta_1$ .

What is Knitr :: Kable?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames.

How do you type MU in RMarkdown?

Math inside RMarkdown If you want a μ in your document, you can just type “mu” instead and forget about the extra complication.


1 Answers

You just need to give some extra escapes. Something in your pipeline ate the \\ characters, so use \\\\ instead:

kable(a, format = "latex", booktabs = TRUE, longtable = TRUE) %>% 
  kable_styling() %>%
  add_header_above(c("n", "$\\\\alpha$", "Estimates for $\\\\alpha$" = 4, 
                     "Estimates for $\\\\beta$" = 4), 
                   align = "l", escape = FALSE)

This gives:

enter image description here

I also changed your T and F to TRUE and FALSE; this is not necessary here, but it is good defensive programming.

like image 120
user2554330 Avatar answered Sep 28 '22 09:09

user2554330