[R] dealing with NA in readBin() and writeBin()

Mike Miller mbmiller+l at gmail.com
Sun Jan 4 23:13:07 CET 2015


The help doc for readBin writeBin tells me this:

Handling R's missing and special (Inf, -Inf and NaN) values is discussed 
in the ‘R Data Import/Export’ manual.

So I go here:

http://cran.r-project.org/doc/manuals/r-release/R-data.html#Special-values

Unfortunately, I don't really understand that.  Suppose I am using 
single-byte integers and I want 255 (binary 11111111) to be translated to 
NA.  Is it possible to do that?  Of course I could always do something 
like this:

X[ X==255 ] <- NA

The problem with that is that I want to process the data on the fly, 
dividing the integer to produce a double in the range from 0 to 2:

X <- readBin( file, what="integer", n=N, size=1, signed=FALSE)/127

It looks like this still works:

X[ X==255/127 ] <- NA

It would be neater if there were some kind of translation option for the 
input stream, like the way GNU tr (Linux/UNIX) works.  I'm looking around 
and not finding such a thing.  I can use gsub() to translate on the fly 
and then coerce back to integer format:

X <- as.integer(gsub("255", NA, readBin( file, what="integer", n=N, size=1, signed=FALSE)))/127

What is your opinion of that tactic?  Is there a better way?  I don't know 
if that has any advantage on the postprocessing tactic above.  Maybe what 
I need is something like gsub() that can operate on numeric values...

X <- numsub(255, NA, readBin( file, what="integer", n=N, size=1, signed=FALSE))/127

...but if that isn't better in terms of speed or memory usage than 
postprocessing like this...

X[ X==255/127 ] <- NA

...then I really don't need it (for this, but it would be good to know 
about).


The na.strings = "NA" functionality of scan() is neat, but I guess that 
doesn't work with the binary read system.  I don't think I can scan the 
readBin input because it isn't a file or stdin.

Mike


More information about the R-help mailing list