Hi
I used write() the other day to save some results.
It seems that write() does not record the full precision of
the objects being written:
> write(pi,file="~/f",ncolumns=1)
> pi.saved <- scan("~/f")
Read 1 item
> dput(pi)
3.14159265358979
> dput(pi.saved)
3.141593
> pi-pi.saved
[1] -3.464102e-07
>
This difficulty was particularly difficult to find because pi.saved
*looks*
the same as pi.
What's going on here?
> R.Version()
$platform
[1] "powerpc-apple-darwin8.7.0"
$arch
[1] "powerpc"
$os
[1] "darwin8.7.0"
$system
[1] "powerpc, darwin8.7.0"
$status
[1] ""
$major
[1] "2"
$minor
[1] "4.0"
$year
[1] "2006"
$month
[1] "10"
$day
[1] "03"
$`svn rev`
[1] "39566"
$language
[1] "R"
$version.string
[1] "R version 2.4.0 (2006-10-03)"
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
write() gotcha
5 messages · Brian Ripley, robin hankin, Roger Bivand
On Mon, 18 Dec 2006, Robin Hankin wrote:
Hi I used write() the other day to save some results.
Why not save()? It is the only way to preserve the results exactly.
It seems that write() does not record the full precision of the objects being written:
write(pi,file="~/f",ncolumns=1)
pi.saved <- scan("~/f")
Read 1 item
dput(pi)
3.14159265358979
dput(pi.saved)
3.141593
pi-pi.saved
[1] -3.464102e-07
This difficulty was particularly difficult to find because pi.saved *looks* the same as pi. What's going on here?
See ?cat, for which write is a wrapper.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 18 Dec 2006, at 08:50, Prof Brian Ripley wrote:
On Mon, 18 Dec 2006, Robin Hankin wrote:
Hi I used write() the other day to save some results.
Why not save()? It is the only way to preserve the results exactly.
It seems that write() does not record the full precision of the objects being written: [snip] What's going on here?
See ?cat, for which write is a wrapper.
Professor Ripley Thanks for this. I'll use save() instead. Also, could we clarify this issue in the write() manpage? Although its behaviour is clear from the code, I'm sure other R users would appreciate a hint or a warning that precision might be lost. It cost me a few hours of worry last night! -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
On Mon, 18 Dec 2006, Robin Hankin wrote:
On 18 Dec 2006, at 08:50, Prof Brian Ripley wrote:
On Mon, 18 Dec 2006, Robin Hankin wrote:
Hi I used write() the other day to save some results.
Why not save()? It is the only way to preserve the results exactly.
It seems that write() does not record the full precision of the objects being written: [snip] What's going on here?
See ?cat, for which write is a wrapper.
Professor Ripley Thanks for this. I'll use save() instead. Also, could we clarify this issue in the write() manpage? Although its behaviour is clear from the code, I'm sure other R users would appreciate a hint or a warning that precision might be lost. It cost me a few hours of worry last night!
Already done (via a xref to cat for the format used)!
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Mon, 18 Dec 2006, Robin Hankin wrote:
Hi I used write() the other day to save some results. It seems that write() does not record the full precision of the objects being written:
> write(pi,file="~/f",ncolumns=1)
> pi.saved <- scan("~/f")
Read 1 item
> dput(pi)
3.14159265358979
> dput(pi.saved)
3.141593
> pi-pi.saved
[1] -3.464102e-07
>
This difficulty was particularly difficult to find because pi.saved *looks* the same as pi. What's going on here?
Use options(digits=) to control the output. ?write says write() is a
wrapper for cat(), so:
options("digits"=16)
cat(pi, "\n")
write(pi,file="~/f",ncolumns=1)
pi.saved <- scan("~/f")
dput(pi.saved)
dput(pi)
looks OK
> R.Version()
$platform [1] "powerpc-apple-darwin8.7.0" $arch [1] "powerpc" $os [1] "darwin8.7.0" $system [1] "powerpc, darwin8.7.0" $status [1] "" $major [1] "2" $minor [1] "4.0" $year [1] "2006" $month [1] "10" $day [1] "03" $`svn rev` [1] "39566" $language [1] "R" $version.string [1] "R version 2.4.0 (2006-10-03)" -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no