R语言开发之二进制文件读写操作

二进制文件是一个文件,其中包含仅以位和字节形式存储的信息(01),它们是不可读的,因为其中的字节转换为包含许多其他不可打印字符的字符和符号,随便我们尝试使用任何文本编辑器读取二进制文件将显示为类似Øð这样的字符。

但是二进制文件必须由特定程序读取才能使用。例如,Microsoft Word程序的二进制文件只能通过Word程序读取到人类可读的形式。这表明,除了人类可读的文本之外,还有更多的信息,如格式化的字符和页码等,它们也与字母数字字符一起存储。最后,二进制文件是一个连续的字节序列。 我们在文本文件中看到的换行符是将第一行连接到下一个的字符。

有时,由其他程序生成的数据需要由R作为二进制文件处理,另外R需要创建可以与其他程序共享的二进制文件,在R中有两个函数用来创建和读取二进制文件,它们分别是:WriteBin()readBin()函数,来看下语法:

writeBin(object, con)
readBin(con, what, n )

参数描述如下:

  • con - 是要读取或写入二进制文件的连接对象。
  • object - 是要写入的二进制文件。
  • what - 是像字符,整数等的模式,代表要读取的字节。
  • n - 是从二进制文件读取的字节数。

我们接下来使用R内置数据“mtcars”创建一个csv文件并将其转换为二进制文件并将其存储为操作系统文件,如下:

#my first R program

# Read the "mtcars" data frame as a csv file and store only the columns "cyl", "am" and "gear".
write.table(mtcars, file = "mtcars.csv",row.names = FALSE, na = "", 
   col.names = TRUE, sep = ",")

# Store 5 records from the csv file as a new data frame.
new.mtcars <- read.table("mtcars.csv",sep = ",",header = TRUE,nrows = 5)

# Create a connection object to write the binary file using mode "wb".
write.filename = file("D:/r_file/binmtcars.dat", "wb")

# Write the column names of the data frame to the connection object.
writeBin(colnames(new.mtcars), write.filename)

# Write the records in each of the column to the file.
writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear), write.filename)

# Close the file for writing so that it can be read by other program.
close(write.filename)

运行上面的文件就会产生一个csv文件和一个dat二进制文件。这个dat文件将所有数据作为连续字节存储, 因此,我们将通过选择列名称和列值的适当值来读取它,如下:

#my first R program

# Create a connection object to read the file in binary mode using "rb".
read.filename <- file("D:/r_file/binmtcars.dat", "rb")

# First read the column names. n = 3 as we have 3 columns.
column.names <- readBin(read.filename, character(),  n = 3)

# Next read the column values. n = 18 as we have 3 column names and 15 values.
read.filename <- file("D:/r_file/binmtcars.dat", "rb")
bindata <- readBin(read.filename, integer(),  n = 18)

# Print the data.
print(bindata)

# Read the values from 4th byte to 8th byte which represents "cyl".
cyldata = bindata[4:8]
print(cyldata)

# Read the values form 9th byte to 13th byte which represents "am".
amdata = bindata[9:13]
print(amdata)

# Read the values form 9th byte to 13th byte which represents "gear".
geardata = bindata[14:18]
print(geardata)

# Combine all the read values to a dat frame.
finaldata = cbind(cyldata, amdata, geardata)
colnames(finaldata) = column.names
print(finaldata)

上述代码演示了几种输出的方式,大家有兴趣的可以自己扩展下。

好啦,本次记录就到这里了。

如果感觉不错的话,请多多点赞支持哦。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luyaran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值