99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R語言處理二進制文件
R語言處理二進制文件
2017-06-18
收藏

R語言處理二進制文件

二進制文件是包含只存儲在比特和字節(jié)形式的信息的文件(0和1)。它們不是人類可讀,將它的字節(jié)轉(zhuǎn)換為包含許多其他非打印字符的字符和符號。嘗試讀取使用任何文本編輯器會顯示類似 ? 和 e 字符的二進制文件。二進制文件必須由特定程序讀取使用。例如,一個微軟Word程序的二進制文件只能由Word程序來讀取以人類可讀形式。這表明,除人類可讀文本,有更大量的字符像和頁碼等的格式信息,其也一起存儲字母數(shù)字字符。最后一個二進制文件是連續(xù)的字節(jié)序列。 我們在一個文本文件中看到的斷點是一個字符加入第一行到下一個!

有時需要由其他程序所產(chǎn)生的數(shù)據(jù),也可以由R為二進制文件進行處理。R語言必需創(chuàng)建可以與其他程序所共享的二進制文件。

R具有兩個函數(shù) WriteBin()和 readBin()創(chuàng)建和讀取二進制文件。

語法

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

以下是所使用的參數(shù)的說明:

con - 是連接對象讀或?qū)懙亩M制文件。

object - 是要被寫入的二進制文件。

what - 是像字符,整數(shù)等代表字節(jié)模式被讀取。

n - 是要從二進制文件中讀取的字節(jié)數(shù)。

示例

我們考慮R內(nèi)置數(shù)據(jù) "mtcars". 首先,我們從它來創(chuàng)建一個CSV文件,并將其轉(zhuǎn)換為二進制文件并將其保存為一個OS文件。接下來,我們將創(chuàng)建的這個二進制文件讀取到R中

寫二進制文件

我們讀出的數(shù)據(jù)幀 "mtcars" 作為一個 CSV 文件,然后把它寫為二進制文件到操作系統(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)

讀二進制文件

上述存儲二進制文件創(chuàng)建的所有數(shù)據(jù)連續(xù)字節(jié)。因此我們將通過選擇的列名的適當(dāng)?shù)闹担约白x取它的列值。

# 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

我們可以看到,我們從二進制文件得到原始數(shù)據(jù)回來到R


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }