Retain numerical precision in an R data frame?

后端 未结 2 1986
情深已故
情深已故 2020-12-11 21:03

When I create a dataframe from numeric vectors, R seems to truncate the value below the precision that I require in my analysis:

data.frame(x=0.99999996)


        
相关标签:
2条回答
  • 2020-12-11 21:53

    Please re-read R FAQ 7.31 and the reference cited therein -- a really famous paper on what everbody should know about floating-point representation on computers.

    The closing quote from Kerngighan and Plauger is also wonderful:

    10.0 times 0.1 is hardly ever 1.0.

    And besides the numerical precision issue, there is of course also how R prints with fewer decimals than it uses internally:

    > for (d in 4:8) print(0.99999996, digits=d)
    [1] 1
    [1] 1
    [1] 1
    [1] 1
    [1] 0.99999996
    > 
    
    0 讨论(0)
  • 2020-12-11 22:01

    If you really want set up R to print its results with utterly unreasonable precision, then use: options(digits=16).

    Note that this does nothing for that accuracy of functions using htese results. It merely changes how values appear when they are printed to the console. There is no rounding of the values as they are being stored or accessed unless you put in more significant digits than the abscissa can handle. The 'digits' option has no effect on the maximal precision of floating point numbers.

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