Controlling number of decimal digits in print output in R

前端 未结 3 730
说谎
说谎 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条回答
  • 2020-11-22 09:36

    The reason it is only a suggestion is that you could quite easily write a print function that ignored the options value. The built-in printing and formatting functions do use the options value as a default.

    As to the second question, since R uses finite precision arithmetic, your answers aren't accurate beyond 15 or 16 decimal places, so in general, more aren't required. The gmp and rcdd packages deal with multiple precision arithmetic (via an interace to the gmp library), but this is mostly related to big integers rather than more decimal places for your doubles.

    Mathematica or Maple will allow you to give as many decimal places as your heart desires.

    EDIT:
    It might be useful to think about the difference between decimal places and significant figures. If you are doing statistical tests that rely on differences beyond the 15th significant figure, then your analysis is almost certainly junk.

    On the other hand, if you are just dealing with very small numbers, that is less of a problem, since R can handle number as small as .Machine$double.xmin (usually 2e-308).

    Compare these two analyses.

    x1 <- rnorm(50, 1, 1e-15)
    y1 <- rnorm(50, 1 + 1e-15, 1e-15)
    t.test(x1, y1)  #Should throw an error
    
    x2 <- rnorm(50, 0, 1e-15)
    y2 <- rnorm(50, 1e-15, 1e-15)
    t.test(x2, y2)  #ok
    

    In the first case, differences between numbers only occur after many significant figures, so the data are "nearly constant". In the second case, Although the size of the differences between numbers are the same, compared to the magnitude of the numbers themselves they are large.


    As mentioned by e3bo, you can use multiple-precision floating point numbers using the Rmpfr package.

    mpfr("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825")
    

    These are slower and more memory intensive to use than regular (double precision) numeric vectors, but can be useful if you have a poorly conditioned problem or unstable algorithm.

    0 讨论(0)
  • 2020-11-22 09:36

    One more solution able to control the how many decimal digits to print out based on needs (if you don't want to print redundant zero(s))

    For example, if you have a vector as elements and would like to get sum of it

    elements <- c(-1e-05, -2e-04, -3e-03, -4e-02, -5e-01, -6e+00, -7e+01, -8e+02)
    sum(elements)
    ## -876.5432
    

    Apparently, the last digital as 1 been truncated, the ideal result should be -876.54321, but if set as fixed printing decimal option, e.g sprintf("%.10f", sum(elements)), redundant zero(s) generate as -876.5432100000

    Following the tutorial here: printing decimal numbers, if able to identify how many decimal digits in the certain numeric number, like here in -876.54321, there are 5 decimal digits need to print, then we can set up a parameter for format function as below:

    decimal_length <- 5
    formatC(sum(elements), format = "f", digits = decimal_length)
    ## -876.54321
    

    We can change the decimal_length based on each time query, so it can satisfy different decimal printing requirement.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题