1

I am getting an output through a program in R as 20.0000000 16.0000000 7.4650426 0.9996772. Here I want to get all the output values in fix 4 places (I don't need to round these figures up to 4th digits). So, I want to use trunc() with option command in beginning. But how???

2 Answers2

2

With options it is possible to set the number of digits, but it uses round and not trunc. The ?options helpfile says:

"digits: controls the number of significant (see signif) digits to print when printing numeric values. It is a suggestion only. Valid values are 1...22 with default 7. See the note in print.default about values greater than 15."

here an example:

> options(digits=16)
> 1/3
[1] 0.3333333333333333
> options(digits=2)
> 1/3
[1] 0.33
> 

This should work in most cases, but some packages may overwrite these settings.

tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
1

We can use sprintf

 sprintf('%.4f', v1)
akrun
  • 874,273
  • 37
  • 540
  • 662