[R] strangely long floating point with write.table()
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Mar 15 12:37:26 CET 2014
On 14-03-14 11:03 PM, Mike Miller wrote:
> On Fri, 14 Mar 2014, Duncan Murdoch wrote:
>
>> On 14-03-14 8:59 PM, Mike Miller wrote:
>>> What I'm using:
>>>
>>> R version 3.0.1 (2013-05-16) -- "Good Sport"
>>> Copyright (C) 2013 The R Foundation for Statistical Computing
>>> Platform: x86_64-unknown-linux-gnu (64-bit)
>>
>> That's not current, but it's not very old...
>>
>>> According to some docs, options(digits) controls numerical precision in
>>> output of write.table(). I'm using the default value for digits:
>>>
>>>> getOption("digits")
>>> [1] 7
>>>
>>> I have a bunch of numbers in a data frame that are only a few digits to
>>> the right of the decimal:
>>
>> That's not enough to reproduce this. Put together a self-contained
>> reproducible example if you're wondering why something behaves as it
>> does. With just a bunch of output, you'll just get uninformed guesses.
>
>
> Thanks for the tip. Here's what I've done:
>
>> data2 <- data[c(94,120),c(18,20,21)]
Thanks, I got the data2.Rdata file. Peter was right, you don't have
what you think you have in that dataframe. See below.
>> save(data2, file="data2.Rdata")
>> q("no")
>
> $ R
>> load("data2.Rdata")
>> data2
> V18 V20 V21
> 94 0.008 0.008 0.000064
> 120 0.023 0.023 0.000529
I'll create a dataframe that looks like yours:
> data3 <- data.frame(V18=c(0.008, 0.023), V20=c(0.008, 0.023),
V21=c(0.000064, 0.000529))
> data3
V18 V20 V21
1 0.008 0.008 0.000064
2 0.023 0.023 0.000529
But it's not the same:
> data2-data3
V18 V20 V21
94 6.938894e-18 6.938894e-18 1.219727e-19
120 -9.020562e-17 -9.020562e-17 -4.119968e-18
I can't tell where these errors crept in; they are likely there in your
"data" object, which you didn't give us. I'd guess as Peter did that
your numbers are the results of computations that introduced rounding error.
Duncan Murdoch
>> write.table(data2, file="data2.txt", sep="\t", row.names=F, col.names=F)
>
> $ cat data2.txt
> 0.00800000000000001 0.00800000000000001 6.40000000000001e-05
> 0.0229999999999999 0.0229999999999999 0.000528999999999996
>
> The data2.Rdata file is attached to this message.
>
> I guess that is enough to reproduce this exact finding. I don't know how
> it works in general.
>
> I don't have a newer version of R available right now. It did the same
> thing on an older version (2.15.1).
>
> Interestingly, on a different machine with an even older version (2.12.2)
> I see something a little different:
>
> 0.008 0.008 6.40000000000001e-05
> 0.0229999999999999 0.0229999999999999 0.000528999999999996
>
> Best,
> Mike
>
More information about the R-help
mailing list