Keep numerical precision in data frame R?

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

data.frame(x=0.99999996) 

returns 1 (*, but see update 1)

I am stuck with fitting spline(x,y) , and two of the x values ​​are 1 due to rounding, while y is changing. I could hack this, but I would rather use a standard solution if one is available.

Example

Here is an example dataset

 d <- data.frame(x = c(0.668732936336141, 0.95351462456867, 0.994620622127435, 0.999602102672081, 0.999987126195509, 0.999999955814133, 0.999999999999966), y = c(38.3026509783688, 11.5895099585560, 10.0443344234229, 9.86152339768516, 9.84461434575695, 9.81648333804257, 9.83306725758297)) 

The following solution works, but I would prefer something less subjective:

 plot(d$x, d$y, ylim=c(0,50)) lines(spline(d$x, d$y),col='grey') #bad fit lines(spline(d[-c(4:6),]$x, d[-c(4:6),]$y),col='red') #reasonable fit 

Update 1

* After posting this question, I understand that this will return 1 , although the data frame still contains the original value, for example.

 > dput(data.frame(x=0.99999999996)) 

returns

 structure(list(x = 0.99999999996), .Names = "x", row.names = c(NA, -1L), class = "data.frame") 

Update 2

After using dput to send this sample dataset and some pointers from Dirk, I see that the problem is not truncating the x values, but within the numerical errors in the model that I used to calculate y . This justifies dropping a few equivalent data points (as in the red line of the example).

+4
source share
2 answers

If you really want to configure R to print the results with completely unreasonable accuracy, use: options(digits=16) .

Note that this does nothing for this precision functions using the htese results. It just changes how the values ​​appear when they are printed on the console. There is no rounding value for the values, because they are stored or available unless you enter more significant numbers than the abscissa can handle. The digits option does not affect the maximum precision of floating point numbers.

+6
source

Please reread R FAQ 7.31 and the link in it, a really well-known article about what you should ever know about floating point representation on computers.

The final quote from Kerngighan and Plauger is also wonderful:

10.0 times 0.1 is hardly ever 1.0.

Furthermore, in addition to numerical precision, of course, there is also the way R prints with fewer decimal places 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 > 
+5
source

Source: https://habr.com/ru/post/1262206/


All Articles