1

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

Abbas
  • 807
  • 7
  • 14
  • Does this answer your question? [How can I subscript names in a table from kable()?](https://stackoverflow.com/questions/43322881/how-can-i-subscript-names-in-a-table-from-kable) – Limey Aug 22 '22 at 11:01
  • No. I want number to be written similar to the link provided in the descriptions – Abbas Aug 22 '22 at 11:06

2 Answers2

2

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


shafee
  • 15,566
  • 3
  • 19
  • 47
0
library(tidyverse)
library(knitr)

tibble(PValue="2 x 10^-16^") %>% kable()

In a chunk in a markdown document produces

enter image description here

As indicated in the potential duplicate above.

Limey
  • 10,234
  • 2
  • 12
  • 32