Alex
Alex

Reputation: 19803

Round numbers in output to show small near-zero quantities as zero

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.

Upvotes: 5

Views: 1848

Answers (3)

smci
smci

Reputation: 33940

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)

Upvotes: 0

Greg Snow
Greg Snow

Reputation: 49640

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

Upvotes: 5

Utkarsh Naiknaware
Utkarsh Naiknaware

Reputation: 126

try

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

Upvotes: 2

Related Questions