R语言 处理二进制文件
在计算机科学领域,文本文件包含了可以被人类轻易理解的数据。它包括字母、数字和其他字符。另一方面,二进制文件包含只有计算机可以解释的1和0。存储在二进制文件中的信息不能被人类阅读,因为其中的字节会转化为包含其他各种不可打印的字符和符号。
,有些时候可能会发生其他程序产生的数据必须被R语言处理为二进制文件的情况。此外,R语言必然负责创建可以与其他程序共享的二进制文件。在二进制文件中可以进行的四个最重要的操作是:
- 创建并向二进制文件写入
- 从二进制文件中读取
- 添加到二进制文件中
- 删除二进制文件
创建和写入二进制文件
通过在 “wb “模式下打开文件,其中w表示写,b表示二进制模式,可以用一个函数 writeBin() 来完成创建和写入二进制文件的工作。
语法: writeBin(object, con)
参数:
object: 一个要写入连接的R对象
con: 一个连接对象或一个命名文件的字符串或一个原始矢量。
示例:
# R program to illustrate
# working with binary file
# Creating a data frame
df = data.frame(
"ID" = c(1, 2, 3, 4),
"Name" = c("Tony", "Thor", "Loki", "Hulk"),
"Age" = c(20, 34, 24, 40),
"Pin" = c(756083, 756001, 751003, 110011)
)
# Creating a connection object
# to write the binary file using mode "wb"
con = file("myfile.dat", "wb")
# Write the column names of the data frame
# to the connection object
writeBin(colnames(df), con)
# Write the records in each of the columns to the file
writeBin(c(dfID, dfName, dfAge, dfPin), con)
# Close the connection object
close(con)
输出:
从二进制文件中读出
从二进制文件的读取可以通过函数 readBin() 进行,在 “rb “模式下打开文件,r表示读取,b表示二进制模式。
语法: readBin(con, what, n )
参数:
con: 一个连接对象或一个命名文件或原始矢量的字符串
what: 一个对象,其模式将给出要读取的矢量的模式,或一个描述模式的长度为1的字符矢量:”数字”、”双数”、”整数”、”int”、”逻辑”、”复杂”、”字符”、”原始” 中的一个
n: 要读取记录的(最大)数目
例如:
# R program to illustrate
# working with binary file
# Creating a connection object
# to read the file in binary mode using "rb".
con = file("myfile.dat", "rb")
# Read the column names
# n = 4 as here 4 column
colname = readBin(con, character(), n = 4)
# Read column values
# n = 20 as here 16 values and 4 column names
con = file("myfile.dat", "rb")
bindata = readBin(con, integer(), n = 20)
# Read the ID values
# as first 1:4 byte for col name
# then values of ID col is within 5 to 8
ID = bindata[5:8]
# Similarly 9 to 12 byte for values of name column
Name = bindata[9:12]
# 13 to 16 byte for values of the age column
Age = bindata[13:16]
# 17 to 20 byte for values of Pincode column
PinCode = bindata[17:20]
# Combining all the values and make it a data frame
finaldata = cbind(ID, Name, Age, PinCode)
colnames(finaldata)= colname
print(finaldata)
输出:
ID Name Age Pin
[1, ] 0 0 0 0
[2, ] 1072693248 1074266112 1074790400 1073741824
[3, ] 0 0 0 0
[4, ] 1073741824 1074790400 1074266112 1072693248
追加到二进制文件中
对二进制文件的追加可以通过相同的函数 writeBin() 进行,以 “ab “模式打开文件,其中a表示追加,b表示二进制模式。
例子:
# R program to illustrate
# working with binary file
# Creating another data frame
# to append with the existing data frame
df = data.frame(
"Salary" = c(100, 200, 300, 400),
"Experience" = c(3, 5, 10, 4)
)
# Creating a connection object
# to append the binary file using mode "ab"
con = file("myfile.dat", "ab")
# append the column names of the data frame
# to the connection object
writeBin(colnames(df), con)
# append the records in each of the columns to the file
writeBin(df$Salary, con)
# Close the connection object
close(con)
输出:
删除二进制文件
在R语言中,可以使用 file.remove() 命令删除二进制文件,然后使用 unlink() 函数解除被删除文件的链接。
例子:
# R program to illustrate
# working with binary file
# Define the file name that will be deleted
fileName <- "myfile.dat"
# Check its existence
if (file.exists(fileName))
# Delete file if it exists
file.remove(fileName)
# Unlink the deleted file
unlink(fileName, recursive = TRUE)