我想将一个二进制文件读入R,但是这个文件的数据行是以二进制代码写入的。因此,它没有一个完整的数据集属于一个列,而是存储为数据行。以下是我的数据样式:
Bytes 1-4: int ID
Byte 5: char response character
Bytes 6-9: int Resp Dollars
Byte 10: char Type 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