[R] Reading single precision floats from binary file

Peter Dalgaard BSA p.dalgaard at pubhealth.ku.dk
Thu Feb 15 18:12:27 CET 2001

Kjetil Kjernsmo <kjetil.kjernsmo at astro.uio.no> writes:

> Dear all,
> I have a few files with binary data written by a C program a friend wrote.
> I allready have program to read these files, Thomas Lumley and Prof Brian
> D Ripley was kind enough to respond to a question to this list earlier
> with some code that works perfectly, so this is really no problem, it is
> more out curiousity.
> Most of my files consists of 40000 single precision floats, and I figured
> the new readBin() function exists for the purpose of reading such files,
> so I tried to use it, but with no success.
> Now, the docs say that what you can read is "numeric", "double",
> "integer", "int", "logical", "complex", "character", and since "single" is
> not on the list, is it simply not the right tool for the job?
> I tried different things, among them, providing single as what,
> changing the size, etc, but it always ends up with reading 20000 records,
> indicating that it does read the file as double precision floats.
> It is not clear to me what the last sentence in the notes for this
> function means, and indeed not quite clear what the function does at all,
> but I was hoping someone can enlighten me.

Works OK for me with the current patch version, except that the n=
parameter to readBin seems to be misdocumented:

zz <- file("testbin", "wb")
writeBin(x, zz, size=4)
system("ls -l testbin")

zz <- file("testbin", "rb")
xx <- readBin(zz, numeric(), n= 999999, size=4)
range(x - xx)

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list