Round numbers in the output for displaying small quantities near zero are zero

I would like the output of my R console to be readable. To do this, I would like R to round all my numbers to the nearest N decimal places. I have some success, but it does not work fully:

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

I would like 0.000000001 to be displayed as just 0. How to do this? Let me clarify: I would like a global fix for the entire R session. I understand that I can start to change things by rounding them, but this is less useful than just setting things for the whole session.

+5
source share
3 answers

See ?options, in particular, the parameters digitsand scipen.

+4
source

to try

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

, , : options('scipen'=+20) and options('digits'=2), round(x,4).

round(x,4) .

Or, you will complete the results of your regression once and save it:

x <- round(x, 4)

... or yes, you should do this every time you show a small amount, if you do not want to store its rounded value. In your case, since you said that small values ​​of almost zero value effectively represent zero, why don't you just round it?

If for some reason you need to keep both the exact and the round version, then do it.

x.rounded <- round(x, 4)
0
source

All Articles