1. 程式人生 > >R語言的資料匯入與匯出

R語言的資料匯入與匯出

福爾·摩斯曾說過:“資料,資料,沒有資料的推理是罪惡!”不過比起有意思的統計分析,資料的匯入與匯出顯得十分的無趣,但是不得不說統計分析的資料匯入與匯出是個讓人沮喪的任務,而且耗時巨大。
今天分享的是R中資料的輸出與一些特定格式的資料讀入。
一、資料的輸出
 R中提供了write.table(),cat()等函式來匯出資料。不過值得指出的是R語言能夠匯出的資料格式是有限的,比如在基本包中,我們能夠匯出資料的格式只有txt,csv。
 現在介紹一下兩個函式的用法:
 write.table(x, file = “”, append =FALSE, quote = TRUE, sep = ” “,
 eol = “\n”, na = “NA”, dec = “.”,row.names = TRUE,

 col.names = TRUE, qmethod = c(“escape”, “double”),
 fileEncoding = “”)
write.csv(…)
 write.csv2(…)
 write.csv(),write.csv2()可以看做write.table()的變體,我們知道write.csv(),與引數sep=“,”的write.table()是等效的。下面介紹幾個常見引數:
 x:資料集
 file:檔案的路徑,包括檔名如:”D:/R/data/data1.csv”
 quote:資料在寫入檔案中時我們常用引號將其隔開,當引數為F時,檔案中的資料不再用引號修飾
 append:是否追加,如果檔名已存在而沒有選擇追加,那麼檔案將會被覆蓋。(覆蓋時是沒有提示的,所以命名需要注意一些。

 cat(… , file = “”, sep = ” “, fill = FALSE, labels = NULL,
 append = FALSE)
 cat()作為一個輸出函式與dos命令差不多,也是將資料集或資料寫入檔案中,常用引數和write.table()類似。
 cat()函式用來輸出,可以把多個引數連線起來再輸出(具有paste()的功能)。

例如:
 > cat(c(“AB”, “C”),c(“E”, “F”), “n”, sep=”/ “)
 AB/ C/ E/ F/ n
 還可以指定一個引數file=給一個檔名,可以把結果寫到指定的檔案中,如: > cat(“i = “, 1, “n”, file=”d:/R/data2.txt”)如果指定的檔案已經存在則原來內容被覆蓋。加上一個append=TRUE引數可以不覆蓋原檔案而是在檔案末尾附加,這很適用於執行中的結果記錄。

 當然cat()的用法比較豐富,也可以用來檢視檔案,與format合用控制輸出格式等。
二、資料的匯入
先介紹R中基本的讀取資料函式read.table()的用法:
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”, text)
read.csv(file, header = TRUE, sep = “,”, quote=”\”", dec=”.”,
fill = TRUE, comment.char=”", …)
read.csv2(file, header = TRUE, sep = “;”, quote=”\”", dec=”,”,
fill = TRUE, comment.char=”", …)
read.delim(file, header = TRUE, sep = “\t”, quote=”\”", dec=”.”,
fill = TRUE, comment.char=”", …)
read.delim2(file, header = TRUE, sep = “\t”, quote=”\”", dec=”,”,
fill = TRUE, comment.char=”", …)
介紹常用引數:
File:檔案路徑,可以用絕對路徑也可以用相對路徑,R的工作目錄你可以使用getwd()來檢視,用setwd()來改。
Header:讀取檔案的第一行是否用作變數名
Sep:分隔符,引數為“,“時等價於read.csv()
Scan()函式也是一個讀取資料比較好的函式,但是引數較為複雜,我們可以說,read.table()函式是scan函式的設定好部分引數的結果。
Read.delim()這個可以讀到剪貼簿的東西,用法為read.delim(clipboard)其他引數與read.table相同。
Read.fwf()讀取固定長度的資料,也可以利用這個特性截去資料的尾巴或者表格的尾巴。
自帶的foreign包可以實現s-plus,sas,spss,stata的資料讀入。以讀stata資料為例:
>Read.dta(“d:/R/data3.dta”)其他引數與read.table也是一樣的。
遺憾的是,基本包與foreign包都沒有辦法讀取excel的資料。但這並不代表我們沒辦法讀取excel的資料。例如我們可以將excel的資料放在剪貼簿中,通過read.delim(clipbroad)來讀取。也可以將excel表格變成csv格式的再處理。最後指出,R中的gdata包的read.xls函式以及RODBC包中也有相應的處理函式。
本文的最後,運用R語言的幫助文件《R資料的匯入與匯出》中的一段話作為結束:“In general, statistical systems like R arenot particularly well suited to manipulations of large-scale data. Some othersystems are better than R at this, and part of the thrust of this manual is tosuggest that rather than duplicating functionality in R we can make anothersystem do the work! (For example Therneau & Grambsch (2000) commented thatthey preferred to do data manipulation in SAS and then use packagesurvival in S for the analysis.)”