Controlling number of decimal digits in print output in R

前端 未结 3 736
说谎
说谎 2020-11-22 08:59

There is an option in R to get control over digit display. For example:

options(digits=10)

is supposed to give the calculation results in 1

3条回答
  •  旧时难觅i
    2020-11-22 09:42

    If you are producing the entire output yourself, you can use sprintf(), e.g.

    > sprintf("%.10f",0.25)
    [1] "0.2500000000"
    

    specifies that you want to format a floating point number with ten decimal points (in %.10f the f is for float and the .10 specifies ten decimal points).

    I don't know of any way of forcing R's higher level functions to print an exact number of digits.

    Displaying 100 digits does not make sense if you are printing R's usual numbers, since the best accuracy you can get using 64-bit doubles is around 16 decimal digits (look at .Machine$double.eps on your system). The remaining digits will just be junk.

提交回复
热议问题