[R] Reading 4-byte float into R from binary file

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jun 24 20:55:31 CEST 2009


On 6/24/2009 2:41 PM, Sean Davis wrote:
> A simple question, I hope....  I have a binary file format that I want to
> slurp into R using readBin.  There are a couple of fields in the file that
> are 4-byte floats.  Since R has no "float" data type (4-byte), I am
> wondering how to go about this task.  I could simply do all the work in C,
> but the format is pretty simple, so I wanted to do it using just R.  I did
> see as.single, but it appears that is for passing things to C and not much
> more.  Any suggestions appreciated.

You can specify the size when reading a float using 
readBin(what="numeric", size=4).  It will read a single, then convert it 
to double to store in R.  You may need to set the endian value too, if 
the byte order is wrong.

Duncan Murdoch




More information about the R-help mailing list