1

I would like R to display floating point numbers only a small amount of digits after the decimal dot. much like "format short" would do in matlab.

Is there a simple way to do that?

eran
  • 14,496
  • 34
  • 98
  • 144
  • 2
    Try searching `[r] format digits` or something similar; you'll find questions like http://stackoverflow.com/q/3245862, http://stackoverflow.com/q/7681756, http://stackoverflow.com/q/5458729 – Aaron left Stack Overflow Nov 17 '11 at 15:11

2 Answers2

6

You can either use an ad-hoc conversion through either formatC, sprintf or format

    ?formatC
    formatC(.000000012, format='fg')
    [1] "0.000000012"

    ?sprintf
    sprintf("%.10f", 0.0000000012)
    [1] "0.0000000012"

    format(.0000012, scientific=FALSE)
    [1] "0.0000012" 

or you set the digits option:

    options(digits=10)

or the scipen option:

    options(scipen=10)
user1052080
  • 430
  • 5
  • 12
3

Just use options("digits"=someSmallNumber). Here is an example using the default and then the value two:

R> set.seed(42);  data.frame(a=rnorm(3), b=runif(3))
          a        b
1  1.370958 0.736588
2 -0.564698 0.134667
3  0.363128 0.656992
R> 
R> options("digits"=2)
R> set.seed(42);  data.frame(a=rnorm(3), b=runif(3))
      a    b
1  1.37 0.74
2 -0.56 0.13
3  0.36 0.66
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725