How to display numbers in scientific notation in ASCII tables?

喜夏-厌秋 提交于 2019-12-12 10:43:44

问题


I am trying to display extremely small numbers (<1E-12) in an ASCII table. (Believe me, I can't find any alternative.) So far I've tried stargazer and xtable. Neither of them seems to work. I can display the numbers in scientific notation with xtable in HTML, but not ASCII. Stargazer seems not have the option to display numbers in scientific notation. Below is an example:

library(stargazer)
example <- data.frame(parameter = letters, value = runif(26, min = 1E-14, max = 5E-14))
stargazer(example, summary = F, type = "text", digits = NA)

All the values are truncated as 0 even if I set the digits option as NA, which is supposed to keep everything. Any help is really appreciated! Thanks!


回答1:


You can convert the value to character. I've done this using the format function, since that makes it easy to control the number of significant figures. I've also used the dplyr package to do the reformatting on the fly:

library(dplyr)

stargazer(example %>% mutate(value = format(value, scientific = TRUE, digits = 4)), 
          summary = FALSE, type = "text")
======================
   parameter   value  
----------------------
1      a     4.456e-14
2      b     2.773e-14
...
25     y     2.982e-14
26     z     1.771e-14
----------------------

You could also avoid dplyr like this:

example$value = format(example$value, scientific = TRUE, digits = 4)

stargazer(example, summary = FALSE, type = "text")



回答2:


Appears you are willing to accept output as character (at least if I'm interpreting your use of "ASCII" correctly). Could also use sprintf which accept Fortran-like specifications:

> formatC(2e-14, digits=16, format="f")
[1] "0.0000000000000200"
> print( formatC(2e-14, digits=16, format="f") , quote=FALSE)
[1] 0.0000000000000200
> cat( formatC(2e-14, digits=16, format="f") )
0.0000000000000200

For scientific notation:

> cat( formatC(2e-14, digits=4, format="e") )
2.0000e-14


来源:https://stackoverflow.com/questions/36584423/how-to-display-numbers-in-scientific-notation-in-ascii-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!