RStudio无法编写科学记数法的结果

时间:2018-03-23 17:50:18

标签: r r-markdown

此代码在点击RStudio中的“knit”时生成一个很好的PDF:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

My favorite number is `r 1 - 0.999`

但以下代码会产生错误

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

My favorite number is `r 1 - 0.9999`

产生错误:

! Missing $ inserted.
<inserted text> 
                $
l.85 My favorite number is 10\times

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43

看起来是因为1 - 0.999的输出为0.001,但1 - 0.9999的输出为1e-04,这是一种不同的表示形式。可以做些什么?

1 个答案:

答案 0 :(得分:0)

其中一个人呢?

---
title: "Untitled"
output: pdf_document
---

My favorite number is `r formatC(1 - 0.9999, format="s")`

My favorite number is `r as.character(1 - 0.9999)`

My favorite number is `r formatC(1 - 0.9999, format="g")`

My favorite number is `r as.character(round(1 - 0.9999, 5))`

<强> 收率:

enter image description here