- 第1节:R语言 概述
- 第2节:R语言 环境设置
- 第3节:R语言 基本语法
- 第4节:R语言 数据类型
- 第5节:R语言 变量
- 第6节:R语言 运算符
- 第7节:R语言 决策
- 第8节:R语言 包
- 第9节:R语言 循环
- 第10节:R语言 数据重塑
- 第11节:R语言 函数
- 第12节:R语言 字符串
- 第13节:R语言 向量
- 第14节:R语言 列表
- 第15节:R语言 矩阵
- 第16节:R语言 数组
- 第17节:R语言 因子
- 第18节:R语言 数据帧
- 第19节:R语言 条形图
- 第20节:R语言 箱线图
- 第21节:R语言 直方图
- 第22节:R语言 折线图
- 第23节:R语言 散点图
- 第24节:R语言 饼状图
- 第25节:R语言 CSV文件
- 第26节:R语言 Excel文件
- 第27节:R语言 二进制文件
- 第28节:R语言 XML文件
- 第29节:R语言 JSON文件
- 第30节:R语言 Web数据
- 第31节:R语言 数据库
- 第32节:R语言 平均值,中位数和模式
- 第33节:R语言 线性回归
- 第34节:R语言 多重回归
- 第35节:R语言 逻辑回归
- 第36节:R语言 标准分布
- 第37节:R语言 二项分布
- 第38节:R语言 泊松回归
- 第39节:R语言 协方差分析
- 第40节:R语言 时间序列分析
- 第41节:R语言 非线性最小二乘
- 第42节:R语言 决策树
- 第43节:R语言 随机森林算法
- 第44节:R语言 生存分析
- 第45节:R语言 卡方检验
R语言 二进制文件
二进制文件是包含仅以位和字节(0和1)的形式存储的信息的文件。它们不是人类可读的,因为它中的字节转换为包含许多其他不可打印字符的字符和符号。尝试使用任何文本编辑器读取二进制文件将显示如Ø和ð的字符。
二进制文件必须由特定程序读取才能使用。例如,Microsoft Word程序的二进制文件只能通过Word程序读取到人类可读的形式。这表示,除了人类可读的文本之外,还有更多的信息,例如字符和页码等的格式化,它们也与字母数字字符一起存储。最后一个二进制文件是一个连续的字节序列。我们在文本文件中看到的换行符是连接第一行到下一行的字符。
有时,由其他程序生成的数据需要由R作为二进制文件处理。另外,R语言是创建可以与其他程序共享的二进制文件所必需的。
R语言有两个函数WriteBin()和readBin()来创建和读取二进制文件。
语法
writeBin(object, con)
readBin(con, what, n )
以下是所使用的参数的描述 -
con 是读取或写入二进制文件的连接对象。
object 是要写入的二进制文件。
what - 是像字符,整数等代表字节模式被读取。
n 是从二进制文件读取的字节数。
例
我们考虑R语言内置数据“mtcars”。 首先,我们从它创建一个csv文件,并将其转换为二进制文件,并将其存储为操作系统文件。 接下来我们读取这个创建的二进制文件。
写入二进制文件
我们将数据帧“mtcars”读取为csv文件,然后将其作为二进制文件写入操作系统。
# 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("/web/com/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)
读取二进制文件
上面创建的二进制文件将所有数据存储为连续字节。 因此,我们将通过选择适当的列名称值和列值来读取它。
# Create a connection object to read the file in binary mode using "rb".
read.filename <- file("/web/com/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("/web/com/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] 7108963 1728081249 7496037 6 6 4
[7] 6 8 1 1 1 0
[13] 0 4 4 4 3 3
[1] 6 6 4 6 8
[1] 1 1 1 0 0
[1] 4 4 4 3 3
cyl am gear
[1,] 6 1 4
[2,] 6 1 4
[3,] 4 1 4
[4,] 6 0 3
[5,] 8 0 3
正如我们所看到的,我们通过读取R中的二进制文件得到原始数据。