编织者的电缆将2.29e-30印刷为"0". [英] knitr's kable is printing 2.29e-30 as "0"
问题描述
代码:
# some data
dat <-
data.frame(
log2fc = c(0.28, 10.82, 8.54, 5.64, 8.79, 6.46),
pvalue = c(0.00e+00, 2.29e-30, 7.02e-30, 4.14e-29, 1.86e-28, 1.78e-27)
)
# observe in markdown format
knitr::kable(dat, format="markdown")
输出:
| log2fc| pvalue|
|------:|------:|
| 0.28| 0|
| 10.82| 0|
| 8.54| 0|
| 5.64| 0|
| 8.79| 0|
| 6.46| 0|
问题:
输出的问题在于,它将最后一列 pvalue
呈现为零.但我想保留与数据框中所见格式相同的格式.我怎么做 ?我已经尝试过各种线程的几种解决方案,但似乎没有任何效果.有人可以指出我正确的方向吗?
The problem with the output is that, it is rendering the last column pvalue
as zeros. But I would want to retain the same format as I see in my dataframe. How do I do that ? I've tried several solutions from various threads but nothing seems to work. Can someone point me to the right direction ?
请不要建议我将pvalue列转换为字符向量.这是一个快速有效的解决方案,但是我不想这样做,因为:
Please do not suggest me to convert the pvalue column into a character vector. That is a quick and dirty solution that works, but I don't want to do that because:
- 我不想弄乱我的数据框.
- 我对为什么在markdown中打印时不保留最后一列的科学格式的原因感兴趣.
- 我有许多表格,每个表格的科学格式都有不同的列,我正在寻找一种自动处理此问题的方法.
推荐答案
kable()
调用基本R函数 round()
,该函数将这些较小的值截断为零除非您将数字设置为非常大的值.但是你可以做到,例如
kable()
calls the base R function round()
, which truncates those small values to zero unless you set digits to a really large value. But you can do that, e.g.
knitr::kable(dat, format = "markdown", digits = 32)
给出
| log2fc| pvalue|
|------:|--------:|
| 0.28| 0.00e+00|
| 10.82| 2.29e-30|
| 8.54| 7.02e-30|
| 5.64| 4.14e-29|
| 8.79| 1.86e-28|
| 6.46| 1.78e-27|
如果您确实希望在某些列中进行常规舍入,则可以为数字指定多个值,例如
If you do want the regular rounding in some columns, you can specify multiple values for digits, e.g.
knitr::kable(dat, format = "markdown", digits = c(1, 32))
| log2fc| pvalue|
|------:|--------:|
| 0.3| 0.00e+00|
| 10.8| 2.29e-30|
| 8.5| 7.02e-30|
| 5.6| 4.14e-29|
| 8.8| 1.86e-28|
| 6.5| 1.78e-27|
这篇关于编织者的电缆将2.29e-30印刷为"0".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!