R:将二进制文件转换为矩阵
R: Convert binary file to matrix
R:我使用 readBin 将一个大的二进制文件(数字,一个数组)上传到 R 中。
我的数据由大于 10000 的值和小于 10000 的值组成,比如
25685, 5, 6, 2, 1, 5, 46789, 6, 2, 9, 44220, 5, 1, 3, 7, 9, 12, 88952, 6, 8,...
如何分离值,以便创建矩阵?
1: 25685, 5, 6, 2, 1, 5
2: 46789, 6, 2, 9
3: 44220, 5, 1, 3, 7, 9, 12
分隔应该像上面那样在最后一个小值和大值之间。
name<-file(".dat", "rb")
size<-(file.info("Path/Name.dat")$size)/8
rB<-readBin(name, numeric(), size, endian="little")
size<-length(rB)
sep<-which(rB>10000) # time steps
len<-diff(sep,1) # lags
len<-c(len,(length(rB)-tail(sep,1)+1)) # last value
mat<-matrix(NA,(size-length(sep)+14),4) # matrix
for(i in 1:(length(sep))) # fill the matrix
{
mat[sep[i]:(sep[i]+len[i]-2),1]<-i
mat[(sep[i]):(sep[i]+len[i]-2),2]<-(1:(len[i]-1))
mat[(sep[i]):(sep[i]+len[i]-2),3]<-rB[sep[i]]
mat[(sep[i]):(sep[i]+len[i]-2),4]<-rB[(sep[i]+1):(sep[i]+len[i]-1)]
}
mat<-mat[which(!is.na(mat[,1])),]
R:我使用 readBin 将一个大的二进制文件(数字,一个数组)上传到 R 中。 我的数据由大于 10000 的值和小于 10000 的值组成,比如
25685, 5, 6, 2, 1, 5, 46789, 6, 2, 9, 44220, 5, 1, 3, 7, 9, 12, 88952, 6, 8,...
如何分离值,以便创建矩阵?
1: 25685, 5, 6, 2, 1, 5
2: 46789, 6, 2, 9
3: 44220, 5, 1, 3, 7, 9, 12
分隔应该像上面那样在最后一个小值和大值之间。
name<-file(".dat", "rb")
size<-(file.info("Path/Name.dat")$size)/8
rB<-readBin(name, numeric(), size, endian="little")
size<-length(rB)
sep<-which(rB>10000) # time steps
len<-diff(sep,1) # lags
len<-c(len,(length(rB)-tail(sep,1)+1)) # last value
mat<-matrix(NA,(size-length(sep)+14),4) # matrix
for(i in 1:(length(sep))) # fill the matrix
{
mat[sep[i]:(sep[i]+len[i]-2),1]<-i
mat[(sep[i]):(sep[i]+len[i]-2),2]<-(1:(len[i]-1))
mat[(sep[i]):(sep[i]+len[i]-2),3]<-rB[sep[i]]
mat[(sep[i]):(sep[i]+len[i]-2),4]<-rB[(sep[i]+1):(sep[i]+len[i]-1)]
}
mat<-mat[which(!is.na(mat[,1])),]