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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言CSV文件
R語言CSV文件
2018-02-21
收藏

R語言CSV文件

R語言中,我們可以從存儲(chǔ)在R環(huán)境外部的文件讀取數(shù)據(jù)。還可以將數(shù)據(jù)寫入由操作系統(tǒng)存儲(chǔ)和訪問的文件。 R可以讀取和寫入各種文件格式,如:csv,excel,xml等。

在本章中,我們將學(xué)習(xí)如何從csv文件中讀取數(shù)據(jù),然后將數(shù)據(jù)寫入csv文件。 該文件應(yīng)該存在于當(dāng)前工作目錄中,以方便R可以讀取它。 當(dāng)然,也可以設(shè)置自己的目錄,并從那里讀取文件。
獲取和設(shè)置工作目錄

可以使用getwd()函數(shù)來檢查R工作區(qū)指向哪個(gè)目錄,使用setwd()函數(shù)設(shè)置新的工作目錄。
# Get and print current working directory.
print(getwd())

# Set current working directory.
# setwd("/web/com")
setwd("F:/worksp/R")

# Get and print current working directory.
print(getwd())
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

[1] "C:/Users/Administrator/Documents"
[1] "F:/worksp/R"
Shell

    注意: 此結(jié)果取決于您的操作系統(tǒng)和您當(dāng)前正在工作的目錄。

作為CSV文件輸入

csv文件是一個(gè)文本文件,其中列中的值用逗號(hào)分隔。假設(shè)下面的數(shù)據(jù)存在于名為input.csv 的文件中。

您可以使用Windows記事本通過復(fù)制和粘貼此數(shù)據(jù)來創(chuàng)建此文件。使用記事本中的另存為所有文件(*.*)選項(xiàng)將文件另存為:input.csv(在目錄:F:/worksp/R 下載)。

id,name,salary,start_date,dept
1,Rick,623.3,2012-01-01,IT
2,Dan,515.2,2013-09-23,Operations
3,Michelle,611,2014-11-15,IT
4,Ryan,729,2014-05-11,HR
 ,Gary,843.25,2015-03-27,Finance
6,Nina,578,2013-05-21,IT
7,Simon,632.8,2013-07-30,Operations
8,Guru,722.5,2014-06-17,Finance
Csv

讀取CSV文件

以下是read.csv()函數(shù)的一個(gè)簡單示例,用于讀取當(dāng)前工作目錄中可用的CSV文件 -

setwd("F:/worksp/R")
data <- read.csv("input.csv")
print(data)
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

> data <- read.csv("input.csv")
> print(data)
  id     name salary start_date       dept
1  1     Rick 623.30 2012-01-01         IT
2  2      Dan 515.20 2013-09-23 Operations
3  3 Michelle 611.00 2014-11-15         IT
4  4     Ryan 729.00 2014-05-11         HR
5 NA     Gary 843.25 2015-03-27    Finance
6  6     Nina 578.00 2013-05-21         IT
7  7    Simon 632.80 2013-07-30 Operations
8  8     Guru 722.50 2014-06-17    Finance
Shell

分析CSV文件

默認(rèn)情況下,read.csv()函數(shù)將輸出作為數(shù)據(jù)幀。這可以很容易地查看到,此外,我們可以檢查列和行的數(shù)量。

setwd("F:/worksp/R")
data <- read.csv("input.csv")

print(is.data.frame(data))
print(ncol(data))
print(nrow(data))
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

[1] TRUE
[1] 5
[1] 8
Shell

當(dāng)我們?cè)跀?shù)據(jù)幀中讀取數(shù)據(jù),可以應(yīng)用所有適用于數(shù)據(jù)幀的函數(shù),如下一節(jié)所述。

獲得最高工資

# Create a data frame.
data <- read.csv("input.csv")

# Get the max salary from data frame.
sal <- max(data$salary)
print(sal)
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

[1] 843.25
Shell

獲得最高工資的人員的詳細(xì)信息

可以使用過濾條件獲取符合特定的行,類似于SQL的where子句。

setwd("F:/worksp/R")
# Create a data frame.
data <- read.csv("input.csv")

# Get the max salary from data frame.
sal <- max(data$salary)

# Get the person detail having max salary.
retval <- subset(data, salary == max(salary))
print(retval)
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

      id    name  salary  start_date    dept
5     NA    Gary  843.25  2015-03-27    Finance
Shell

獲取IT部門的所有人員

# Create a data frame.
data <- read.csv("input.csv")

retval <- subset( data, dept == "IT")
print(retval)
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

       id   name      salary   start_date   dept
1      1    Rick      623.3    2012-01-01   IT
3      3    Michelle  611.0    2014-11-15   IT
6      6    Nina      578.0    2013-05-21   IT
Shell

獲取IT部門薪水在600以上的人員

setwd("F:/worksp/R")
# Create a data frame.
data <- read.csv("input.csv")

info <- subset(data, salary > 600 & dept == "IT")
print(info)
R
當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

       id   name      salary   start_date   dept
1      1    Rick      623.3    2012-01-01   IT
3      3    Michelle  611.0    2014-11-15   IT
Shell

獲得在2014年或以后入職的人員

setwd("F:/worksp/R")
# Create a data frame.
data <- read.csv("input.csv")

retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
print(retval)
R
當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
       id   name     salary   start_date    dept
3      3    Michelle 611.00   2014-11-15    IT
4      4    Ryan     729.00   2014-05-11    HR
5     NA    Gary     843.25   2015-03-27    Finance
8      8    Guru     722.50   2014-06-17    Finance
Shell
寫入CSV文件
R可以從現(xiàn)有數(shù)據(jù)幀中來創(chuàng)建csv文件。write.csv()函數(shù)用于創(chuàng)建csv文件。 該文件在工作目錄中創(chuàng)建。參考以下示例代碼 -
setwd("F:/worksp/R")
# Create a data frame.
data <- read.csv("input.csv")
retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
# print(retval)
# Write filtered data into a new file.
write.csv(retval,"output.csv")
newdata <- read.csv("output.csv")
print(newdata)
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -

  X      id   name      salary   start_date    dept
1 3      3    Michelle  611.00   2014-11-15    IT
2 4      4    Ryan      729.00   2014-05-11    HR
3 5     NA    Gary      843.25   2015-03-27    Finance
4 8      8    Guru      722.50   2014-06-17    Finance
Shell
這里列X來自數(shù)據(jù)集更新器。在編寫文件時(shí)可以使用其他參數(shù)來刪除它。
setwd("F:/worksp/R")
# Create a data frame.
data <- read.csv("input.csv")
retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))

# Write filtered data into a new file.
write.csv(retval,"output.csv", row.names = FALSE)
newdata <- read.csv("output.csv")
print(newdata)
R

當(dāng)我們執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果 -
      id    name      salary   start_date    dept
1      3    Michelle  611.00   2014-11-15    IT
2      4    Ryan      729.00   2014-05-11    HR
3     NA    Gary      843.25   2015-03-27    Finance
4      8    Guru      722.50   2014-06-17    Finance

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

若不方便掃碼,搜微信號(hào):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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }