R語(yǔ)言對(duì)CSV文件操作實(shí)例講解
在 R 語(yǔ)言中,我們可以從存儲(chǔ)在 R 語(yǔ)言環(huán)境外的文件中讀取數(shù)據(jù)。 我們還可以將數(shù)據(jù)寫(xiě)入將被操作系統(tǒng)存儲(chǔ)和訪問(wèn)的文件。 R 語(yǔ)言可以讀取和寫(xiě)入各種文件格式,如csv
,excel
,xml
等。
在本章中,我們將學(xué)習(xí)從csv
文件讀取數(shù)據(jù),然后將數(shù)據(jù)寫(xiě)入csv
文件。 該文件應(yīng)該存在于當(dāng)前工作目錄中,以便 R 語(yǔ)言可以讀取它。 當(dāng)然我們也可以設(shè)置我們自己的目錄并從那里讀取文件。
獲取和設(shè)置工作目錄
您可以使用getwd()
函數(shù)檢查R語(yǔ)言工作區(qū)指向的目錄。 您還可以使用setwd()
函數(shù)設(shè)置新的工作目錄。
# Get and print current working directory. print(getwd()) # Set current working directory. setwd("/web/com") # Get and print current working directory. print(getwd())
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[1] "/web/com/1441086124_2016" [1] "/web/com"
此結(jié)果取決于您的操作系統(tǒng)和您當(dāng)前工作的目錄。
輸入為CSV文件
csv 文件是一個(gè)文本文件,其中列中的值由逗號(hào)分隔。 讓我們考慮名為input.csv
的文件中出現(xiàn)的以下數(shù)據(jù)。
您可以通過(guò)復(fù)制和粘貼此數(shù)據(jù)使用 Windows 記事本創(chuàng)建此文件。 使用記事本中的保存為所有文件(*.*)
選項(xiàng)將文件保存為input.csv
。
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文件
以下是read.csv()
函數(shù)的一個(gè)簡(jiǎn)單示例,用于讀取當(dāng)前工作目錄中可用的 CSV 文件
data <- read.csv("input.csv") print(data)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
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
分析CSV文件
默認(rèn)情況下,read.csv()
函數(shù)將輸出作為數(shù)據(jù)幀。 這可以容易地如下檢查。 此外,我們可以檢查列和行的數(shù)量。
data <- read.csv("input.csv") print(is.data.frame(data)) print(ncol(data)) print(nrow(data))
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[1] TRUE [1] 5 [1] 8
一旦我們讀取數(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)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
[1] 843.25
獲取具有最高工資的人的詳細(xì)信息
我們可以獲取滿足特定過(guò)濾條件的行,類(lèi)似于SQL where
子句。
# 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)
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果
id name salary start_date dept 5 NA Gary 843.25 2015-03-27 Finance
獲取所有的 IT 部門(mén)員工的信息
# Create a data frame. data <- read.csv("input.csv") retval <- subset( data, dept == "IT") print(retval)
當(dāng)我們執(zhí)行上面的代碼,它產(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
獲得工資大于600的 IT 部門(mén)的人員
# Create a data frame. data <- read.csv("input.csv") info <- subset(data, salary > 600 & dept == "IT") print(info)
當(dāng)我們執(zhí)行上面的代碼,它產(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
獲得2014年或之后加入的人
# Create a data frame. data <- read.csv("input.csv") retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01")) print(retval)
當(dāng)我們執(zhí)行上面的代碼,它產(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
寫(xiě)入CSV文件
R 語(yǔ)言可以創(chuàng)建csv
文件形式的現(xiàn)有數(shù)據(jù)幀。 write.csv()
函數(shù)用于創(chuàng)建csv
文件。 此文件在工作目錄中創(chuàng)建。
# 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") newdata <- read.csv("output.csv") print(newdata)
當(dāng)我們執(zhí)行上面的代碼,它產(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
這里列 X 來(lái)自數(shù)據(jù)集newper
。 這可以在寫(xiě)入文件時(shí)使用附加參數(shù)刪除。
# 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)
當(dāng)我們執(zhí)行上面的代碼,它產(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
到此這篇關(guān)于R語(yǔ)言對(duì)CSV文件操作實(shí)例講解的文章就介紹到這了,更多相關(guān)R語(yǔ)言CSV文件操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R語(yǔ)言實(shí)現(xiàn)支持向量機(jī)SVM應(yīng)用案例
本文主要介紹了R語(yǔ)言實(shí)現(xiàn)支持向量機(jī)SVM應(yīng)用案例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08R語(yǔ)言 數(shù)據(jù)表匹配和拼接 merge函數(shù)的使用
這篇文章主要介紹了R語(yǔ)言 數(shù)據(jù)表匹配和拼接 merge函數(shù)的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03R語(yǔ)言 解決安裝ggplot2報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了R語(yǔ)言 解決安裝ggplot2報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04R語(yǔ)言中循環(huán)的相關(guān)知識(shí)詳解
這篇文章主要為大家詳細(xì)介紹了R語(yǔ)言中循環(huán)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)R語(yǔ)言有一定的幫助,感興趣的可以了解一下2023-03-03R語(yǔ)言編程數(shù)學(xué)分析重讀微積分微分學(xué)原理運(yùn)用
這篇文章主要介紹了R語(yǔ)言編程數(shù)學(xué)分析重讀微積分微分學(xué)的原理運(yùn)用,有需要的朋友可以借鑒參考下,希望能夠有=有所幫助,祝大家多多進(jìn)步2021-10-10R語(yǔ)言RcppEigen計(jì)算點(diǎn)乘與矩陣乘法連乘算法錯(cuò)誤解決
這篇文章主要為大家介紹了RcppEigen計(jì)算點(diǎn)乘與矩陣乘法時(shí)發(fā)生連乘計(jì)算錯(cuò)誤的解決方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11R語(yǔ)言which函數(shù)介紹及Rcpp改寫(xiě)詳解
有的時(shí)候我們需要找到一個(gè)數(shù)據(jù)子向量中的位置,我們就可以使用which函數(shù),下面這篇文章主要給大家介紹了關(guān)于R語(yǔ)言which函數(shù)介紹及Rcpp改寫(xiě)的相關(guān)資料,需要的朋友可以參考下2022-07-07R語(yǔ)言 實(shí)現(xiàn)將1對(duì)多數(shù)據(jù)與1對(duì)1數(shù)據(jù)互換
這篇文章主要介紹了R語(yǔ)言 實(shí)現(xiàn)將1對(duì)多數(shù)據(jù)與1對(duì)1數(shù)據(jù)互換的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03R語(yǔ)言中dnorm,pnorm,qnorm和rnorm的區(qū)別淺析
正在學(xué)習(xí)R語(yǔ)言統(tǒng)計(jì)學(xué)的小伙伴,可能會(huì)被各種專(zhuān)有名詞所困擾,下面這篇文章主要給大家介紹了關(guān)于R語(yǔ)言中dnorm,pnorm,qnorm和rnorm區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-12-12