R:在Linux上的readBin和writeBin [英] R: readBin and writeBin on linux
问题描述
size
参数是更好还是省略。无论如何,加载的输入看起来不像 out
: out< -seq(1,50,2)
## write
write< -file('〜/ output.txt','wb')
writeBin out,con = write,size = 4)
close(write)
## read
read< -file('〜/ output.txt','rb')
readBin(con = read,what = numeric(),n = length(out))
#[1] 3.200001e + 01 3.276801e + 04 1.048576e + 06 1.677722e + 07 1.006633e + 08 4.026532e + 08 1.610613e + 09 6.442452e + 09 1.503239e + 10 3.006478e + 10 6.012955e + 10 1.202591e + 11
接近(读)
这是一个有效的例子:
R> dat< - seq(1,50,2)
R> dat
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
R>
我们只是将对象写入二进制连接 - 省略了第三个参数:
R> con< - file(/ tmp / foo.bin,wb)
R> writeBin(dat,con)
R> close(con)
R>
然后可以读回:
R> con< - file(/ tmp / foo.bin,rb)
R> dat2< - readBin(con,what =numeric,n = length(dat))
R> dat2
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
R>
R> all.equal(dat,dat2)
[1] TRUE
但是,您需要存储长度。我已经使用了一个内部文件格式,它首先写入关于行,列,格式号的固定(已知)数的整数,然后读取/写入总共行*列数值。我们也把这个包装成gzip-ed文件连接。
你甚至可以把它推广到像数据框一样写列 - 但是如果R是你唯一的读者/编写者然后通过 save()
已经存在的序列化更好。
I want to write (and later retrieve) data in binary format. I am trying to get a minimal example to work at least to the level of the smell test (read input should look like written output), but I haven't gotten it just, and consistently right. My machine is a linux with little endian, but since that is constant here, I ommitted it from the calls. I was also not sure if it was better to specify the size
argument in the write, or leave it out. At any rate, the loaded input doesn't look like out
:
out<-seq(1,50,2)
##write
write<-file('~/output.txt','wb')
writeBin(out,con=write,size=4)
close(write)
##read
read<-file('~/output.txt','rb')
readBin(con=read,what=numeric(),n=length(out))
# [1] 3.200001e+01 3.276801e+04 1.048576e+06 1.677722e+07 1.006633e+08 4.026532e+08 1.610613e+09 6.442452e+09 1.503239e+10 3.006478e+10 6.012955e+10 1.202591e+11
close(read)
Here is a worked example:
R> dat <- seq(1,50,2)
R> dat
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
R>
We just write the object to a binary connection -- omitting your third parameter:
R> con <- file("/tmp/foo.bin", "wb")
R> writeBin(dat, con)
R> close(con)
R>
And can then read back:
R> con <- file("/tmp/foo.bin", "rb")
R> dat2 <- readBin(con, what="numeric", n=length(dat))
R> dat2
[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
R>
R> all.equal(dat, dat2)
[1] TRUE
However, you need to store the length. I have used an internal file format which first writes a fixed (known) number of integers about rows, cols, "format number", ... and then read / writes a total of rows * cols numerics. We also wraps this into a gzip-ed file connection.
You could even generalize this to writing columns as in a data frame -- but if R is your only reader / writer then the already exisiting serialization via save()
is better.
这篇关于R:在Linux上的readBin和writeBin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!