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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言做數(shù)據(jù)分析(8)—數(shù)據(jù)分析師
R語言做數(shù)據(jù)分析(8)—數(shù)據(jù)分析師
2014-11-13
收藏


R語言做數(shù)據(jù)分析(8)—數(shù)據(jù)分析師


數(shù)據(jù)的輸入與輸出之READ函數(shù)


read.table() 函數(shù)
1、用于讀入表格(表)類型的數(shù)據(jù),同時生成數(shù)據(jù)框對象。
2、讀入的數(shù)據(jù)要求有規(guī)則的分隔符,默認有:空格、TAB、換行符、回車符;其它的分隔符,通過sep=來進行指定。

read.table(file, header = FALSE, sep = "", quote = "\"'",
           dec = ".", row.names, col.names,
           as.is = !stringsAsFactors,
           na.strings = "NA", colClasses = NA, nrows = -1,
           skip = 0, check.names = TRUE, fill = !blank.lines.skip,
           strip.white = FALSE, blank.lines.skip = TRUE,
           comment.char = "#",
           allowEscapes = FALSE, flush = FALSE,
           stringsAsFactors = default.stringsAsFactors(),
           fileEncoding = "", encoding = "unknown")

  1. file:指定讀入的文件,或者文件所在地址;
  2. header:是否讀入列名,默認是不讀入;
  3. sep:來進行指定分隔符:讀入的數(shù)據(jù)要求有規(guī)則的分隔符,可以是:空格、TAB、換行符、回車符;
  4. as.is:讀入的字符是否轉換成因子,默認所有讀入的字符都轉換成因子;
  5. colClasses:指定列的數(shù)據(jù)類型格式
  6. header=TRUE 第一行是否是列的名稱,默認是TRUE
  7. stringsAsFactors= 是否字符轉化成因子,默認是true
  8. row.names=c()指定各行的名稱
  9. col.names=C()指定列的名稱,如果讀入是文件沒有頭,可以指定
  10. skip=N 從文件第幾行開始讀入數(shù)據(jù)
  11. nrows=N 讀入的最大行數(shù)
  12. na.strings=c()指定什么樣的字符表示值缺少
  13. comment.char=‘’ 指定評論的開始字符,默認是#
  14. dec= 指定小數(shù)點數(shù)
  15. encoding=指定非non-ASCII的編碼規(guī)則

例如:
demo_3<-read.table('e:/demo_3.txt',header=T)
 
read.fwf()函數(shù)
1、適用用于讀入數(shù)據(jù)相應沒有相應的分隔符,但是讀入的數(shù)據(jù)字段長度是固定長度。
2、數(shù)據(jù)導入R后,生成列表對象。
讀入固定分隔長度的數(shù)據(jù);

read.fwf(file, widths, header = FALSE, sep = "\t",
         skip = 0, row.names, col.names, n = -1,
         buffersize = 2000)

  1. file:指定讀入的文件,或者文件所在地址;
  2. widths:指定分隔的長度,可以等于向量指定不同的分隔;
  3. buffersize:一次最大的讀入行數(shù);
  4. n:讀入數(shù)據(jù)的行數(shù),默認為無數(shù);

例如:在這個數(shù)據(jù)中,前面的3個字符與接下來的3個數(shù)字表示名稱、得分,因為二個字段之間沒有分隔符號,但其長度是固定的,所以適合用本函數(shù)。
ABC123%$12
TEX124@#12
y o14 @@#
read.fwf('e:/demo_1.txt',widths=c(3,3),col.names=c('name','score'));
w <- readline()函數(shù)
1、用于程序的交互,根據(jù)輸入的條件來判斷下一步執(zhí)行的方向;
2、通過鍵盤讀入一行數(shù)據(jù);
例如:根據(jù)輸入的來判斷后續(xù)程序的執(zhí)行流程

Demo_2<-function()
{
         input<-readline("DO you think R is hard to learn,Please give your choice:Y or N ")
         if(input=="Y")
                   cat("Come on; Spent more time.\n")
         else
                   cat("Good!")
}
 

Demo_2()

Readlines() 函數(shù)
1、控制讀入的數(shù)據(jù)行數(shù),非批處理,有點類似于數(shù)據(jù)庫中的指標操作,可對文件中的數(shù)據(jù)逐行操作。2、這個對于讀入日志類的數(shù)據(jù)很有用。例如:通過對讀入數(shù)據(jù)的每行來判斷是否有需要的數(shù)據(jù),有再對數(shù)據(jù)進行處理;tips:該數(shù)據(jù)配合R中的正則表達式相關函數(shù),對于處理不規(guī)則的數(shù)據(jù)很強大。
例如:
1、  與文件demo_1建立連接
con<- file("demo_3","r")
2、指定每次執(zhí)行只讀入一行;
RC<-readLines(con,n=1)
3、關閉聯(lián)接
close(con)
說明:
1、如果讀到文件的最后,則length(RC)=0;EOF文件最后返回的空值。
2、N控制每次讀入幾行;http://www.3lll3.cn/
3、當讀到最后要重新開始的時間:seek(con=c,where=0),返回當前指標所有的位置


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

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

數(shù)據(jù)分析師考試動態(tài)
數(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(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括: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); }