将二进制文件读入R

时间:2012-11-13 00:54:14

标签: r binaryfiles

我正在尝试将二进制文件读入R,但此文件包含以二进制代码编写的数据行。因此,它没有一个属于一列的完整数据集,而是存储为数据行。这是我的数据:

字节1-4:int ID 字节5:字符响应字符 字节6-9:int Resp美元 字节10:char类型char

任何人都可以帮我弄清楚如何将这个文件读入R?

嗨,大家好,

这是迄今为止我尝试过的代码。我尝试了几件但成效有限的事情。不幸的是,我无法在公共网站上发布任何数据,道歉。我对R比较新,所以在如何改进代码方面需要一些帮助。提前致谢。

> 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 个答案:

答案 0 :(得分:4)

您可以将文件作为原始文件打开,然后发出readBin或readChar命令以获取每一行。随时将每个值附加到一列。

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

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

绕过这个街区:

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.

然后创建数据框。

见这里:http://www.ats.ucla.edu/stat/r/faq/read_binary.htm