Knitr kable prints 2.29e-30 as "0"

CODE:

# 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")

      

OUTPUT:

| log2fc| pvalue|
|------:|------:|
|   0.28|      0|
|  10.82|      0|
|   8.54|      0|
|   5.64|      0|
|   8.79|      0|
|   6.46|      0|

      

Problem:

The problem with the output is that it returns the last column pvalue

as zeros. But I would like to keep the same format as in my framework. How should I do it? I've tried several solutions from different threads but nothing seems to work. Can anyone point me in the right direction?

Please do not suggest me converting a pvalue column to a character vector. This is a quick and dirty solution that works, but I don't want to do this because:

  • I don't want to mess with my file frame.
  • I am interested in the reason why the scientific format of the last column is not preserved when printed in markdown.
  • I have many tables with different columns with scientific format, I am looking for a way that automatically handles this problem.
+3


source to share


1 answer


kable()

calls the base R function round()

, which truncates these small values ​​to zero unless you set the numbers to a really large value. But you can do this for example.

knitr::kable(dat, format = "markdown", digits = 32)

      

which gives



| 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 want regular rounding in some columns, you can specify multiple values ​​for the numbers, for example.

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|

      

+4


source







All Articles