R:在Linux上的readBin和writeBin [英] R: readBin and writeBin on linux

查看:619
本文介绍了R:在Linux上的readBin和writeBin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以二进制格式写入(以后检索)数据。我试图得到一个最小的例子,至少到嗅觉测试的水平(读输入应该看起来像书面的输出),但我没有得到它,一贯的权利。我的机器是一个小端的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆