我正在嘗試將二進制文件讀入R,但是此文件具有以二進制代碼寫入的數據行。所以它沒有一組屬於一列的完整數據,而是存儲爲數據行。這裏是我的數據是什麼樣子:將二進制文件讀入R
字節1-4:INT ID 字節5:焦炭響應特性 字節6-9:INT RESP美元 字節10: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
你可以發佈數據文件的前幾行嗎? – Seth
將它作爲readBin的原始字節讀取,並將其推入與文件相同的nrow矩陣中,然後將readBin從多組列中讀入。它很快。這基本上爲更復雜的格式,刪除所有標題的東西和slurp了:http://stackoverflow.com/questions/12931979/reading-binary-files-in-r – mdsumner
提供的文件,我會放在一起一個例子。 – mdsumner