Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用knitr和kable()生成的表格控制零值的科学表示法和格式_R_Knitr_Scientific Notation - Fatal编程技术网

使用knitr和kable()生成的表格控制零值的科学表示法和格式

使用knitr和kable()生成的表格控制零值的科学表示法和格式,r,knitr,scientific-notation,R,Knitr,Scientific Notation,我有一个小表格,它显示在knitr在Rstudio中生成的pdf文档中 大多数值都是小数字,所以科学记数法很好。然而,我有一些零也会显示在科学记数法中 有没有一种方法可以将这些表示为常规零 这是一张图片: 这个表是用kable(数据)生成的,这里有一种方法。一般的想法是,首先按照您想要的方式格式化数字(小数位数等),然后将零的数字更改为“0” 在发布kable(数据) --- title: "Untitled" author: "Author" date: "August 17, 2016"

我有一个小表格,它显示在knitr在Rstudio中生成的pdf文档中

大多数值都是小数字,所以科学记数法很好。然而,我有一些零也会显示在科学记数法中

有没有一种方法可以将这些表示为常规零

这是一张图片:


这个表是用kable(数据)生成的,这里有一种方法。一般的想法是,首先按照您想要的方式格式化数字(小数位数等),然后将零的数字更改为“0”

在发布
kable(数据)
---
title: "Untitled"
author: "Author"
date: "August 17, 2016"
output:
  pdf_document
---

```{r setup, include=FALSE}
library(knitr)
```

```{r}
# Some data
df = mtcars[1:5,c(1,3:4)]/1e7
rownames(df) = NULL

# Set a few values to zero
df[2:3,2] = 0
df[c(1,3),1] = 0

# Look at the starting data
kable(df)
```

```{r}
## Reformat table so that zeros are rendered as "0"

# First, format data so that every number is rendered with two decimal places
df = lapply(df, format, digits=3)

# If a value is zero, change string representation to "0" instead of "0.00e+00"
df = sapply(df, function(i) ifelse(as.numeric(i) == 0, "0", i))

kable(df, align=rep('r',3))
```