1.R数据的保存与加载
可通过save()函数保存为.Rdata文件,通过load()函数将数据加载到R中。
- > a <- 1:10
- > save(a,file='d://data//dumData.Rdata')
- > rm(a)
- > load('d://data//dumData.Rdata')
- > print(a)
- [1] 1 2 3 4 5 6 7 8 9 10
2.CSV文件的导入与导出
下面创建df1的数据框,通过函数write.csv()保存为一个.csv文件,然后通过read.csv()将df1加载到数据框df2中。
- > var1 <- 1:5
- > var2 <- (1:5)/10
- > var3 <- c("R and","Data Mining","Examples","Case","Studies")
- > df1 <- data.frame(var1,var2,var3)
- > names(df1) <- c("VariableInt","VariableReal","VariableChar")
- > write.csv(df1,"d://data//dummmyData.csv",row.names = FALSE)
- > df2 <- read.csv("d://data//dummmyData.csv")
- > print(df2)
- VariableInt VariableReal VariableChar
- 1 1 0.1 R and
- 2 2 0.2 Data Mining
- 3 3 0.3 Examples
- 4 4 0.4 Case
- 5 5 0.5 Studies
3.通过ODBC导入与导出数据
RODBC提供了ODBC数据库的连接。
3.1从数据库中读取数据
odbcConnect()建立一个数据库连接,sqlQuery()向数据库发送一个SQL查询,odbcClose()关闭数据库连接。
- library(RODBC)
- connection <- odbcConnect(dsn="servername",uid="userid",pwd="******")
- query <- "SELECT * FROM lib.table WHERE ..."
- myData <- sqlQuery(connection, query, errors = TRUE)
- odbcClose(connection)
sqlSave()和sqlUpdate()用于写入或更新一个ODBC数据库表。
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.
|
请发表评论