Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display numbers in scientific notation in ASCII tables?

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!

like image 989
Jin Avatar asked Apr 12 '16 21:04

Jin


People also ask

How do you show a number in scientific notation?

The Scientific format displays a number in exponential notation, replacing part of the number with E+n, in which E (exponent) multiplies the preceding number by 10 to the nth power. For example, a 2-decimal scientific format displays 12345678901 as 1.23E+10, which is 1.23 times 10 to the 10th power.

How do you convert from standard form to scientific notation?

Steps for Converting Scientific Notation to Standard FormStep 1: Identify the exponent in the power of 10. Step 2: Move the decimal that many places to the right if the exponent is positive and to the left if the exponent is negative. Step 3: Fill in any empty spaces with zeros.

How do you format numbers in labels and scientific notation?

The proper format for scientific notation is a x 10^b where a is a number or decimal number such that the absolute value of a is greater than or equal to one and less than ten or, 1 ≤ |a| < 10. b is the power of 10 required so that the scientific notation is mathematically equivalent to the original number.


1 Answers

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")
like image 175
eipi10 Avatar answered Sep 29 '22 20:09

eipi10