在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ R語言二進(jìn)制文件
R語言列表
R語言隨機(jī)森林
R語言矩陣
R語言邏輯回歸
R語言數(shù)據(jù)幀
R語言數(shù)據(jù)重塑
R語言概述
R語言包
R語言字符串
R語言CSV文件
R語言運(yùn)算符
為什么使用R語言做統(tǒng)計(jì)?
R語言Web數(shù)據(jù)
R語言二進(jìn)制文件
R語言XML文件
R語言JSON文件
R語言因子
R語言容易學(xué)習(xí)嗎?
R語言基礎(chǔ)語法
R語言向量
R語言教程
R語言正態(tài)分布
R語言平均值,中位數(shù)和眾數(shù)
R語言變量
R語言條形圖
R語言決策樹
R語言開發(fā)環(huán)境安裝配置
R語言數(shù)組
R語言數(shù)據(jù)類型
R語言非線性最小二乘法
R語言直方圖
R語言卡方檢驗(yàn)
R語言泊松回歸
R語言決策結(jié)構(gòu)
R語言盒形圖(箱形圖)
R語言協(xié)方差分析
R語言二項(xiàng)分布
R語言餅狀圖
R語言循環(huán)
R語言散點(diǎn)圖
R語言線性回歸
R語言時(shí)間序列分析
R語言線形圖
R語言在現(xiàn)實(shí)中的應(yīng)用
R語言生存分析
R語言多元(多重)回歸
R語言函數(shù)
R語言Excel文件
R語言連接數(shù)據(jù)庫(MySQL)

R語言二進(jìn)制文件

二進(jìn)制文件是一個(gè)文件,其中包含僅以位和字節(jié)形式存儲(chǔ)的信息(01)。它們不可讀,因?yàn)槠渲械淖止?jié)轉(zhuǎn)換為包含許多其他不可打印字符的字符和符號(hào)。嘗試使用任何文本編輯器讀取二進(jìn)制文件將顯示為類似?e這樣的字符。

二進(jìn)制文件必須由特定程序讀取才能使用。例如,Microsoft Word程序的二進(jìn)制文件只能通過Word程序讀取到人類可讀的形式。這表明,除了人類可讀的文本之外,還有更多的信息,如格式化的字符和頁碼等,它們也與字母數(shù)字字符一起存儲(chǔ)。最后二進(jìn)制文件是一個(gè)連續(xù)的字節(jié)序列。 我們?cè)谖谋疚募锌吹降膿Q行符是將第一行連接到下一個(gè)的字符。

有時(shí),由其他程序生成的數(shù)據(jù)需要由R作為二進(jìn)制文件處理。 另外R需要?jiǎng)?chuàng)建可以與其他程序共享的二進(jìn)制文件。

R有兩個(gè)函數(shù)用來創(chuàng)建和讀取二進(jìn)制文件,它們分別是:WriteBin()readBin()函數(shù)。

語法

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ù)。

實(shí)例

這里考慮使用R內(nèi)置數(shù)據(jù)“mtcars”。 首先,我們從它創(chuàng)建一個(gè)csv文件并將其轉(zhuǎn)換為二進(jìn)制文件并將其存儲(chǔ)為操作系統(tǒng)文件。接下來將這個(gè)二進(jìn)制文件讀入R中。

1. 寫入二進(jìn)制文件

我們將數(shù)據(jù)幀“mtcars”讀為csv文件,然后將其作為二進(jìn)制文件寫入操作系統(tǒng)。參考以下代碼實(shí)現(xiàn) -

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

2. 讀取二進(jìn)制文件

上面創(chuàng)建的二進(jìn)制文件將所有數(shù)據(jù)作為連續(xù)字節(jié)存儲(chǔ)。 因此,我們將通過選擇列名稱和列值的適當(dāng)值來讀取它。

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