5

I would like the output of my R console to look readable. To this end, I would like R to round all my numbers to the nearest N decimal places. I have some success but it doesn't work completely:

> options(scipen=100, digits=4)
> .000000001
[1] 0.000000001
> .1
[1] 0.1
> 1.23123123123
[1] 1.231

I would like the 0.000000001 to be displayed as simply 0. How does one do this? Let me be more specific: I would like a global fix for the entire R session. I realize I can start modifying things by rounding them but it's less helpful than simply setting things for the entire session.

smci
  • 32,567
  • 20
  • 113
  • 146
Alex
  • 19,533
  • 37
  • 126
  • 195
  • 2
    It seems to me that displaying `0.000000001` as `0` isn't readable, it's just misleading (what's wrong with scientific notation for such cases?) – David Robinson Mar 31 '13 at 18:15
  • when I run a regression, my variables tend to be `scale`d so that 0.000000001 is in fact really zero (insignificant tstat) but it being displayed as 1e-9 is just not readable when you have a bunch of numbers that are being displayed some with 1e-9, some with 1e0 (which actually are significant) – Alex Mar 31 '13 at 18:18
  • 2
    how are you outputting your regression? Whatever function you use, chances are it has a print command inside of it. Inside that print command, insert a `round(x, 4)` as necessary. – Ricardo Saporta Mar 31 '13 at 18:22
  • @RicardoSaporta and Greg Snow combined gives you the answer. – smci Nov 24 '15 at 00:53

3 Answers3

5

Look at ?options, specifically the digits and scipen options.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • 1
    can you be more specific? obviously i have looked at those already as you can see from the question... – Alex Apr 11 '13 at 20:33
2

try

sprintf("%.4f", 0.00000001)
[1] "0.0000"
0

Combine what Greg Snow and Ricardo Saporta already gave you to get the right answer: options('scipen'=+20) and options('digits'=2) , combined with round(x,4) .

round(x,4) will round small near-zero quantities.

Either you round off the results of your regression once and store it:

x <- round(x, 4)

... or else yes, you have to do that every time you display the small quantity, if you don't want to store its rounded value. In your case, since you said small near-zero quantities effectively represent zero, why don't you just round it?

If for some reason you need to keep both the precise and the rounded versions, then do.

x.rounded <- round(x, 4)
smci
  • 32,567
  • 20
  • 113
  • 146