欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言實現二進制文件讀寫操作

 更新時間:2021年03月11日 10:26:51   作者:luyaran  
這篇文章主要介紹了R語言實現二進制文件讀寫操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

二進制文件是一個文件,其中包含僅以位和字節(jié)形式存儲的信息(0和1),它們是不可讀的,因為其中的字節(jié)轉換為包含許多其他不可打印字符的字符和符號,隨便我們嘗試使用任何文本編輯器讀取二進制文件將顯示為類似Ø和ð這樣的字符。

但是二進制文件必須由特定程序讀取才能使用。例如,Microsoft Word程序的二進制文件只能通過Word程序讀取到人類可讀的形式。這表明,除了人類可讀的文本之外,還有更多的信息,如格式化的字符和頁碼等,它們也與字母數字字符一起存儲。最后,二進制文件是一個連續(xù)的字節(jié)序列。 我們在文本文件中看到的換行符是將第一行連接到下一個的字符。

有時,由其他程序生成的數據需要由R作為二進制文件處理,另外R需要創(chuàng)建可以與其他程序共享的二進制文件,在R中有兩個函數用來創(chuàng)建和讀取二進制文件,它們分別是:WriteBin()和readBin()函數,來看下語法:

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

參數描述如下:

  • con - 是要讀取或寫入二進制文件的連接對象。
  • object - 是要寫入的二進制文件。
  • what - 是像字符,整數等的模式,代表要讀取的字節(jié)。
  • n - 是從二進制文件讀取的字節(jié)數。

我們接下來使用R內置數據“mtcars”創(chuàng)建一個csv文件并將其轉換為二進制文件并將其存儲為操作系統(tǒng)文件,如下:

#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文件將所有數據作為連續(xù)字節(jié)存儲, 因此,我們將通過選擇列名稱和列值的適當值來讀取它,如下:

#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)

上述代碼演示了幾種輸出的方式,大家有興趣的可以自己擴展下。

到此這篇關于R語言實現二進制文件讀寫操作的文章就介紹到這了,更多相關R語言 二進制文件讀寫操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • R語言中邏輯回歸知識點總結

    R語言中邏輯回歸知識點總結

    在本篇文章里小編給大家總結了關于R語言中邏輯回歸知識點相關內容,有需要的朋友們跟著學習下。
    2021-05-05
  • R語言 如何刪除指定變量或對象

    R語言 如何刪除指定變量或對象

    這篇文章主要介紹了R語言刪除指定變量或對象的操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言作圖:坐標軸的設置方式

    R語言作圖:坐標軸的設置方式

    這篇文章主要介紹了R語言作圖:坐標軸的設置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • R語言 實現矩陣相乘100次

    R語言 實現矩陣相乘100次

    這篇文章主要介紹了R語言 實現矩陣相乘100次的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言-如何截取變量中指定位置的若干個字符

    R語言-如何截取變量中指定位置的若干個字符

    這篇文章主要介紹了R語言截取變量中指定位置的若干個字符,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 詳解R語言apply系列函數的使用

    詳解R語言apply系列函數的使用

    R語言的循環(huán)效率并不高,所以并不推薦循環(huán)以及循環(huán)嵌套。為了實現循環(huán)功能的情況下,兼顧效率,R語言提供了apply系列函數,用于對規(guī)則的數據進行函數式的迭代處理,下面就來和大家聊聊它們的使用吧
    2023-03-03
  • R語言which函數介紹及Rcpp改寫詳解

    R語言which函數介紹及Rcpp改寫詳解

    有的時候我們需要找到一個數據子向量中的位置,我們就可以使用which函數,下面這篇文章主要給大家介紹了關于R語言which函數介紹及Rcpp改寫的相關資料,需要的朋友可以參考下
    2022-07-07
  • R語言入門使用RStudio制作包含Rcpp代碼的R包

    R語言入門使用RStudio制作包含Rcpp代碼的R包

    這篇文章主要為大家介紹了R語言入門使用RStudio來制作包含Rcpp代碼的R包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-11-11
  • R語言中data.frame的常用操作總結

    R語言中data.frame的常用操作總結

    這篇文章主要介紹了R語言中data.frame的常用操作總結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 如何改變R語言默認存儲包的路徑

    如何改變R語言默認存儲包的路徑

    這篇文章主要介紹了改變R語言默認存儲包的路徑操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論