You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rmarkdown表格中以科学计数法(×10格式)显示数值

解决Rmarkdown表格中P-value科学计数法渲染问题

你需要在knitr::kable()中添加escape = FALSE参数,让LaTeX公式代码不被转义,从而正确渲染成带×10上标的格式。以下是修改后的完整代码:

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) 

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) %>%  # 关键参数:禁止转义LaTeX代码
   kable_styling(font_size = 8,
                 latex_options = "hold_position",
                 full_width = F) %>% 
   row_spec(0, bold = T) %>%
   column_spec(1, border_right = F)

核心说明

  • escape = FALSE是解决问题的关键:默认情况下kable会转义特殊字符,导致LaTeX的\times和上标语法无法生效,添加该参数后,LaTeX公式会被正确解析,渲染出你期望的×10⁻¹⁶样式。
  • 保持P-value列的字符串为标准LaTeX格式:$<2 \\times 10^{-16}$中的双反斜杠是R字符串中表示单个反斜杠的正确写法,对应LaTeX中的\times命令。

内容的提问来源于stack exchange,提问作者Abbas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 12:06:29