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 保存為簡單文本
Usage
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.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
>options(stringsAsFactors=FALSE)
>setwd("C:/data"); HousePrice <- read.table(file="houses.dat")
如果明確數據第一行做表頭,則使用header選項
>HousePrice <- read.table("houses.dat", header=TRUE)
options(stringsAsFactors=FALSE)
Exp <- read.table(file="cer.v05.csv", header=TRUE, sep=",", row.names= 1)
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)
fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)
txt <- "Hallo\nWorld"
writeLines(txt, "outfile.txt")
or
txt <- c("Hallo", "World")
writeLines(txt, "outfile.txt")
2 另外一個寫文件的方法是sink,不會在行末加換行符
sink(output_fasta)
cat(mystr)
sink()
sink("outfile.txt") cat("hello") cat("\n") cat("world") sink()
> cat("Hello",file="outfile.txt",sep="\n") > cat("World",file="outfile.txt",append=TRUE)
file.show("outfile.txt")
line="blah text blah blah etc etc" write(line,file="myfile",append=TRUE)
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.
readChar(con, nchars, useBytes = FALSE)
writeChar(object, con, nchars = nchar(object, type = "chars"), eos = "", useBytes = FALSE)
# read
fileName <- 'foo.txt'
oldstring<-readChar(fileName, file.info(fileName)$size)
REF: