Re: [R] Writing to a file with fixed precision

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Tue 11 Oct 2005 - 22:36:38 EST

On Tue, 2005-10-11 at 08:42 +0100, Prof Brian Ripley wrote:
> On Mon, 10 Oct 2005, Marc Schwartz wrote:
>
> > On Mon, 2005-10-10 at 19:50 -0400, Richard Hedger wrote:
> >> Hi,
> >> I'm trying to ouput to a filled with a fixed precision:
> >> eg. if I have data x=c(1.0,1.4,2.0), I want to be able to ouput the following to a file:
> >> 1.00000000000000
> >> 1.40000000000000
> >> 2.00000000000000
> >> I was wondering if there was a function to do this in R?
> >> Thanks,
> >> Richard
> >
> > It is possible that someone has written such a function somewhere.

>
> It's called format().
>
> x <- c(1.0,1.4,2.0)
> write(format(x, nsmall=14))
>
> does this.

Indeed. Sorry, I was not clear in my use of words. I was thinking along the lines of a single function call such as:

  write.fmt(x, file = "data.txt", ndigits = 14)

It would of course be easy enough to create such a wrapper using existing functions.

I was aware of format(), but for some reason had in the back of my mind that the use of 'nsmall' was not consistent in the decimal place output based upon prior experience.

The result of which led me to use the vectorized formatC() to control such output. I then shifted to using sprintf(), when in 2.1.0, it was vectorized.

Using format() also adds the benefit of having methods for matrices, etc., as opposed to sprintf().

Thanks,

Marc



R-help@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 Received on Tue Oct 11 22:41:24 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:40:17 EST