[R] storage and single-precision

William Dunlap wdunlap at tibco.com
Thu Sep 8 17:39:57 CEST 2011


Use gzcon() to make a compressed connection and any
function that write to a connection will write compressed
data.  E.g.,

  > con <- gzcon(file("tempfile.junk", "wb"))
  > x <- as.integer(rep(c(-127, 1, 127), c(3,2,1)))
  > writeBin(x, con, size=1)
  > close(con)
  > q("no")
  bill:158% zcat tempfile.junk | od --format d1
  0000000 -127 -127 -127    1    1  127
  0000006
(In this tiny example the gzip'ed file is bigger than the
equivalent one, but it is gzip'ed.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mike Miller
> Sent: Thursday, September 08, 2011 8:14 AM
> To: Duncan Murdoch
> Cc: R-Help List
> Subject: Re: [R] storage and single-precision
> 
> On Thu, 8 Sep 2011, Duncan Murdoch wrote:
> 
> > On 11-09-07 6:25 PM, Mike Miller wrote:
> >
> >> I'm getting the impression from on-line docs that R cannot work with
> >> single-precision floating-point numbers, but that it has a pseudo-mode
> >> for single precision for communication with external programs.
> >>
> >> I don't mind that R is using doubles internally, but what about
> >> storage? If all I need to store is single-precision (32-bit), can I do
> >> that?  When it is read back into R it can be converted from single to
> >> double (back to 64-bit).
> >>
> >> Furthermore, the data are numbers from 0.000 to 2.000 with no missing
> >> values that could be stored just as accurately as unsigned 16-bit
> >> integers from 0 to 2000.  That would be the best plan for me.
> >
> >
> > writeBin is quite flexible in converting between formats if you just
> > want to store them on disk.  To use nonstandard formats in memory will
> > require external support; it's not easy.
> 
> 
> Thanks.  I can see now that writeBin will store unsigned 16-bit integers,
> which is what I want.  There is one other issue -- with save() I'm allowed
> to use compression (e.g., gzip), but there doesn't seem to be a
> compression option in writeBin.  Is there a way to get the best of both
> worlds?  The data are highly nonrandom and at most 11 bits will be used
> per integer, so the compression ratio should be pretty good, if I can have
> one.
> 
> Mike
> 
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list