R語(yǔ)言對(duì)二進(jìn)制文件操作詳解
二進(jìn)制文件是包含僅以位和字節(jié)(0和1)的形式存儲(chǔ)的信息的文件。它們不是人類可讀的,因?yàn)樗械淖止?jié)轉(zhuǎn)換為包含許多其他不可打印字符的字符和符號(hào)。嘗試使用任何文本編輯器讀取二進(jìn)制文件將顯示如Ø和ð的字符。
二進(jìn)制文件必須由特定程序讀取才能使用。例如,Microsoft Word程序的二進(jìn)制文件只能通過Word程序讀取到人類可讀的形式。這表示,除了人類可讀的文本之外,還有更多的信息,例如字符和頁(yè)碼等的格式化,它們也與字母數(shù)字字符一起存儲(chǔ)。最后一個(gè)二進(jìn)制文件是一個(gè)連續(xù)的字節(jié)序列。我們?cè)谖谋疚募锌吹降膿Q行符是連接第一行到下一行的字符。
有時(shí),由其他程序生成的數(shù)據(jù)需要由R作為二進(jìn)制文件處理。另外,R語(yǔ)言是創(chuàng)建可以與其他程序共享的二進(jìn)制文件所必需的。
R語(yǔ)言有兩個(gè)函數(shù)WriteBin()和readBin()來(lái)創(chuàng)建和讀取二進(jìn)制文件。
語(yǔ)法
writeBin(object, con) readBin(con, what, n )
以下是所使用的參數(shù)的描述
- con 是讀取或?qū)懭攵M(jìn)制文件的連接對(duì)象。
- object 是要寫入的二進(jìn)制文件。
- what - 是像字符,整數(shù)等代表字節(jié)模式被讀取。
- n 是從二進(jìn)制文件讀取的字節(jié)數(shù)。
例
我們考慮R語(yǔ)言內(nèi)置數(shù)據(jù)“mtcars”。 首先,我們從它創(chuàng)建一個(gè)csv文件,并將其轉(zhuǎn)換為二進(jìn)制文件,并將其存儲(chǔ)為操作系統(tǒng)文件。 接下來(lái)我們讀取這個(gè)創(chuàng)建的二進(jìn)制文件。
寫入二進(jìn)制文件
我們將數(shù)據(jù)幀“mtcars”讀取為csv文件,然后將其作為二進(jìn)制文件寫入操作系統(tǒng)。
# 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)
讀取二進(jìn)制文件
上面創(chuàng)建的二進(jìn)制文件將所有數(shù)據(jù)存儲(chǔ)為連續(xù)字節(jié)。 因此,我們將通過選擇適當(dāng)?shù)牧忻Q值和列值來(lái)讀取它。
# 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)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果和圖表
[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中的二進(jìn)制文件得到原始數(shù)據(jù)。
到此這篇關(guān)于R語(yǔ)言對(duì)二進(jìn)制文件操作詳解的文章就介紹到這了,更多相關(guān)R語(yǔ)言二進(jìn)制文件操作實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R語(yǔ)言編程數(shù)學(xué)分析重讀微積分理解極限算法
這篇文章主要為大家介紹了R語(yǔ)言編程重讀微積分?jǐn)?shù)學(xué)分析理解極限算法的詳細(xì)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式
這篇文章主要介紹了R的ggplot2畫圖,去除灰色陰影和網(wǎng)格的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-04-04
R語(yǔ)言向量知識(shí)點(diǎn)及實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于R語(yǔ)言向量知識(shí)點(diǎn)及實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
R語(yǔ)言中矩陣matrix和數(shù)據(jù)框data.frame的使用詳解
這篇文章主要介紹了R語(yǔ)言中矩陣matrix和數(shù)據(jù)框data.frame的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
R語(yǔ)言學(xué)習(xí)筆記之lm函數(shù)詳解
這篇文章主要介紹了R語(yǔ)言學(xué)習(xí)筆記之lm函數(shù)詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
R語(yǔ)言繪圖學(xué)習(xí)教程VennDiagram繪制venn
這篇文章主要為大家介紹了R語(yǔ)言繪圖學(xué)習(xí)教程VennDiagram繪制venn的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

