Reading binary file into R

2019-02-19 02:06发布

问题:

I'm trying to read a binary file into R but this file has rows of data written in binary code. So it doesnt have one full set of data belonging to one column instead it is stored as rows of data. Here's what my data looks like:

Bytes 1-4: int ID Byte 5: char response character Bytes 6-9: int Resp Dollars Byte 10: char Type char

anyone can help me figure out how to read this file into R?

Hi Guys,

Here's the code ive tried so far. I tried a couple of things with limited success. Unfortunately, I cant post any of the data on public sites, apologies. I’m relatively new to R, so I need some help in terms of how to improve the code. Thanks in advance.

> binfile = file("File Location", "rb")
> IDvals = readBin(binfile, integer(), size=4, endian = "little")
> Responsevals = readBin(binfile, character (), size = 5)
> ResponseDollarsvals = readBin (binfile, integer (), size = 9, endian= "little")
Error in readBin(binfile, integer(), size = 9, endian = "little") : 
  size 9 is unknown on this machine
> Typevals = readBin (binfile, character (), size=4)
> binfile1= cbind(IDvals, Responsevals, ResponseDollarsvals, Typevals)
> dimnames(binfile1)[[2]]
[1] "IDvals"            "Responsevals"        "ResponseDollarsvals" "Typevals"  

> colnames(binfile1)=binfile
Error in `colnames<-`(`*tmp*`, value = 4L) : 
  length of 'dimnames' [2] not equal to array extent

回答1:

You could open the file as a raw file, then issue readBin or readChar commands to get each line. Append each value to a column as you go.

my.file <- file('path', 'rb')

id <- integer(0)
response <- character(0)
...

Loop around this block:

id = c(id, readBin(my.file, integer(), size = 4, endian = 'little'))
response = c(response, readChar(my.file, 1))
...
readChar(my.file, size = 1) # For UNIX newlines.  Use size = 2 for Windows newlines.

Then create your data frame.

See here: http://www.ats.ucla.edu/stat/r/faq/read_binary.htm



标签: r binaryfiles