Tomas
Tomas

Reputation: 59575

Use scientific notation only when needed

I am printing the following data.frame, and I hate that numbers like 349 and 57 are printed in scientific notation, see the column '2.5%':

                  Mean          SD   P.value         2.5%        97.5% Time-series SE      psrf  psrf CI
b[1]        0.23852320  0.05569869 0.000 *** 1.402605e-01   0.35707199   0.0017095134 0.9999016 1.000003
b[2]        0.15168616  0.05645316 0.000 *** 4.155178e-02   0.26495673   0.0019034327 1.0031425 1.008611
b[3]        0.05807863  0.03886247 0.000 *** 3.315073e-07   0.14805463   0.0014782918 1.0053901 1.013379
b[4]        0.04353199  0.03252788 0.000 *** 2.194049e-08   0.12328049   0.0012016604 1.0023942 1.004400
Nsuper    404.22088889 30.97052370 0.000 *** 3.490000e+02 470.00000000   1.0517872620 1.0028975 1.010676
N[1]       97.52644444 24.28870256 0.000 *** 5.700000e+01 151.00000000   0.8022620656 1.0004164 1.001592
N[2]      126.41844444 22.53806189 0.000 *** 8.600000e+01 175.00000000   0.9843877604 1.0014555 1.004210

In the column '97.5%' it is OK because there were no small numbers to force scientific notation. How do I force to use scientific notation only for numbers where it is needed, within one vector/data.frame column? The desired result is something like this:

                  Mean          SD   P.value         2.5%        97.5% Time-series SE      psrf  psrf CI
b[1]        0.23852320  0.05569869 0.000 ***   1.4026e-01   0.35707199   0.0017095134 0.9999016 1.000003
b[2]        0.15168616  0.05645316 0.000 ***   4.1551e-02   0.26495673   0.0019034327 1.0031425 1.008611
b[3]        0.05807863  0.03886247 0.000 ***   3.3150e-07   0.14805463   0.0014782918 1.0053901 1.013379
b[4]        0.04353199  0.03252788 0.000 ***   2.1940e-08   0.12328049   0.0012016604 1.0023942 1.004400
Nsuper    404.22088889 30.97052370 0.000 *** 349.00000000 470.00000000   1.0517872620 1.0028975 1.010676
N[1]       97.52644444 24.28870256 0.000 ***  57.00000000 151.00000000   0.8022620656 1.0004164 1.001592
N[2]      126.41844444 22.53806189 0.000 ***  86.00000000 175.00000000   0.9843877604 1.0014555 1.004210

I tried to use options(scipen = xx) but this can only disable the scientific notation for all numbers, which I don't want: it is ridiculous for very small numbers like 1.0e-08.

Reproducible example: print(data.frame(a=1:3, b=c(1e-8, 230, 380))).

Upvotes: 3

Views: 215

Answers (1)

PKumar
PKumar

Reputation: 11128

You can try prettyNum not sure if it solves your problem completely but it does work for your reproducible set. While formatting it changes everything to character. Make sure that, you are using the default of scipen, if you have changed it like options(scipen=999) etc, then you have to do options(sicpen=NULL) to make below work as given.

prettyNum(x)

> prettyNum(x)
[1] "1e-12" "230"   "380"

Where x <- c(0.000000000001, 230, 380)

EDIT After Change in reproducible set:

df <- data.frame(a=1:3, b=c(1e-8, 230, 380))

You can try this with sapply, This will apply to every column and would change the data type to character:

data.frame(sapply(df, prettyNum), stringsAsFactors=FALSE)

> data.frame(sapply(df, prettyNum))
  a     b
1 1 1e-08
2 2   230
3 3   380

Upvotes: 3

Related Questions