Search code examples
rr-markdownknitrscientific-notationkableextra

Writing numbers scientifically ( with × 10 ) in Rmarkdown tables


I would like to mention small numbers as × 10 in the Rmarkdown table, similar to the code here.

Here is the code:

library(knitr)
library(kableExtra)
library(janitor)

x <- cbind(c("Term", "s(MERRA_WS)", "s(MERRA_T)", "s(MERRA_P)", "s(MERRA_WD)"),
           c( "edf", 
              round(6.69852,2),
              round(6.69852,2),
              round(6.69852,2),
              round(6.69852,2)),
          c("Statistics",
            round(6.69852,2),
              round(6.69852,2),
              round(6.69852,2),
              round(6.69852,2)),
           c("P-value", "$< 2 \\times 10^{-16}$","$< 2 \\times 10^{-16}$","$< 2 \\times 10^{-16}$","$< 2 \\times 10^{-16}$"),
          c("Significance", "***", "***","***","***")
          )

x <- as.data.frame(x) # set as dataframe

x <- janitor::row_to_names(x, 1, remove_rows_above = FALSE) %>%  clean_names() # set the 1st row as header

rownames(x) <- NULL

x %>% 
   knitr::kable(digits = 2, 
               caption = "\\label{table:par-4} Estimates of parametric parameters for the modified model 4", align = c("l","c","c","c","c"),col.names = c("Term","edf", "Statistic", "P-value", "Significance"),
               format = "latex", booktabs = T) %>%  kable_styling(font_size = 8,
                                                  latex_options = "hold_position",
                                                  full_width = F) %>% 
   row_spec(0, bold = T) %>%
  column_spec(1, border_right = F)

and here is the output in which the P-Value column doesn't convert to scientific form: enter image description here

I would like the output in the P-Value column looks like this:

enter image description here


Solution

  • One trick could be using escape = FALSE in kable(). But you have to be careful about this. As per the documentation,

    When escape = FALSE, you have to make sure that special characters will not trigger syntax errors in LaTeX or HTML.

    And that's why we have to manually escape the underscores in s(MERRA_WS) as s(MERRA\\_WS).


    ---
    title: "P value formatting"
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    ```{r}
    library(knitr)
    library(kableExtra)
    library(janitor)
    library(rstatix)
    
    x <- cbind(c("Term", "s(MERRA\\_WS)", "s(MERRA\\_T)", "s(MERRA\\_P)", "s(MERRA\\_WD)"),
               c( "edf", 
                  round(6.69852,2),
                  round(6.69852,2),
                  round(6.69852,2),
                  round(6.69852,2)),
              c("Statistics",
                round(6.69852,2),
                  round(6.69852,2),
                  round(6.69852,2),
                  round(6.69852,2)),
               c("P-value", 
                 "$< 2 \\times 10^{-16}$", 
                 "$< 2 \\times 10^{-16}$", 
                 "$< 2 \\times 10^{-16}$", 
                 "$< 2 \\times 10^{-16}$"),
              c("Significance", "***", "***","***","***")
              )
    
    x <- as.data.frame(x) 
    
    x <- janitor::row_to_names(x, 1, remove_rows_above = FALSE) %>%  clean_names()
    
    rownames(x) <- NULL
    
    x %>% 
       knitr::kable(digits = 2, 
           caption = "\\label{table:par-4} Estimates of parametric parameters for the modified model 4",
           align = c("l","c","c","c","c"),
           col.names = c("Term","edf", "Statistic", "P-value", "Significance"),
           format = "latex", 
           booktabs = T,
           escape = FALSE) %>%  
      kable_styling(font_size = 8,
                    latex_options = "hold_position",
                    full_width = F) %>% 
       row_spec(0, bold = T) %>%
      column_spec(1, border_right = F)
    ```
    

    kable table with formatted p value