R語言資料檔案讀寫

來源:互聯網
上載者:User
轉載自:http://www.cnblogs.com/emanlee/archive/2012/12/04/2802352.html
R語言資料儲存與讀取

1 首先用getwd() 獲得目前的目錄,用setwd("C:/data")設定目前的目錄

 

2 資料儲存

建立資料框d

>d <- data.frame(obs = c(1, 2, 3), treat = c("A", "B", "A"), weight = c(2.3, NA, 9))

2.1 儲存為簡單文本

>write.table(d, file = "c:/data/foo.txt", row.names = F, quote = F) # 空格分隔

>write.table(d, file = "c:/data/foo.txt", row.names = F, quote = F, sep="\t")  # tab 分隔的檔案

2.2 儲存為逗號分割文本

>write.csv(d, file = "c:/data/foo.csv", row.names = F, quote = F)

2.3 儲存為R格式檔案

>save(d, file = "c:/data/foo.Rdata")

2.4 儲存工作空間鏡像

>save.image( ) = save(list =ls(all=TRUE), file=".RData")

 

3 資料讀取

讀取函數主要有:read.table( ), scan( ) ,read.fwf( ),readLines().

3.1 用 read.table( ) 讀 "c:\data” 下houses.dat

>setwd("C:/data"); HousePrice <- read.table(file="houses.dat")

如果明確資料第一行做表頭,則使用header選項

>HousePrice <- read.table("houses.dat", header=TRUE)

read.table( ) 變形有: read.csv( ),read.csv2( ), read.delim( ), read.delim2( ).前兩讀取逗號分割資料,後兩個讀取其他分割符資料。

3.2  用scan( ) 比read.table( ) 更靈活。

但要指定 變數類型:如:C:\data\data.dat:

M 65 168

M 70 172

F 54 156

F 58 163

>mydata <- scan("data.dat", what = list("", 0, 0))

>mydata <- scan("data.dat", what = list(Sex="", Weight=0, Height=0))

3.3 用read.fwf( )讀取檔案中一些固定寬度資料

如:C:\data\data.txt:

A1.501.2

A1.551.3

B1.601.4

>mydata <- read.fwf("data.txt", widths=c(1, 4, 3), col.names=c("X","Y","Z"))

 

4 excel格式資料讀取

4.1 利用剪下板

選擇excel資料,再用(CTRL+C)複製。在R中鍵入命令:

>mydata <- read.delim("clipboard")

4.2 使用程式包 RODBC.

如: c:\data\body.xls

Sex Weight Height

M 65 168

M 70 172

F 54 156

F 58 163

> library(RODBC)

> z <- odbcConnectExcel("c:/data/body.xls")

> foo <- sqlFetch(z, "Sheet1")

> close(z)

 

  To an Excel Spreadsheet 儲存為Excel檔案:

library(xlsx)    #   注意: 軟體包需要安裝
write.xlsx(mydata, "c:/mydata.xlsx") #   參考: https://danganothererror.wordpress.com/2012/02/12/write-data-frame-to-excel-file/

The WriteXLS function from the WriteXLS package (link: http://cran.r-project.org/web/packages/WriteXLS/index.html) can write data to Excel.

Alternatively, write.xlsx from the xlsx package (link: http://cran.r-project.org/web/packages/xlsx/) will also work.

 

注意:

1 writeLines 會在最後一行/或者每行末尾加一個分行符號

# fileConn<-file(output_fasta)
# writeLines(mystr, fileConn)
# close(fileConn)

2 另外一個寫檔案的方法是sink,不會在行末加分行符號

sink(output_fasta)
cat(mystr)
sink()

 

write is a wrapper for cat, which gives further details on the format used.

save for writing any R objects, write.table for data frames, and scan for reading data.

 

 

REF:

http://www.statmethods.net/input/exportingdata.html

http://hi.baidu.com/wuyu466/item/d46edcd96c2838e955347f2c

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.