R获取数据
三种途径:键盘 读取本地 数据库
view()
edit() fix()
#Reading file from Database
install.packages(“RODBC”)
读取文本文件
read.table()
read.table(‘file’, header = FALSE, sep = “”, quote = “”'",
dec = “.”, numerals = c(“allow.loss”, “warn.loss”, “no.loss”),
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 = FALSE,
fileEncoding = “”, encoding = “unknown”, text, skipNul = FALSE)
默认sep为空格分隔
注意首行header是否为列名
读取空间: skip~nrows
na.strings 指定文本中的缺失值
stringsAsFactors 是否将字符串读取为因子类型
> x <- read.table ("input.txt")
> x <- read.table ("input.csv",sep=",",header = T)
> head(x)
X mpg cyl disp hp drat wt qsec vs am gear carb
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
x <- read.table ("input.csv",sep=",",header = T,skip = 50,nrows = 100,
na.strings = " ")
x <- read.table ("input.csv",sep=",",header = T,skip = 50,nrows = 100,
stringsAsFactors = F)
其它读取纯文本文件函数如 read.csv()等都是简化了分隔符的子函数
读取剪切板 readClipboard()
面对非标准的文件格式:
readlines() – 同py 返回每行为字符串
scan 用于复杂格式文件的处理
> readLines('scan.txt')
[1] "one\t2\t3\tfour\t5\t6" "one\t2\t3\tfour\t5\t6" "one\t2\t3\tfour\t5\t6"
[4] "one\t2\t3\tfour\t5\t6" "one\t2\t3\tfour\t5\t6" "one\t2\t3\tfour\t5\t6"
[7] "one\t2\t3\tfour\t5\t6" "one\t2\t3\tfour\t5\t6" "one\t2\t3\tfour\t5\t6"
[10] "one\t2\t3\tfour\t5\t6" ""
> x <- scan("scan.txt",what=list (character(3),numeric(0),numeric(0)))
Read 20 records
> x <- scan("scan.txt",what=list (X1=character(3),X2=numeric(0),X3=numeric(0)))
Read 20 records
> x
$X1
[1] "one" "four" "one" "four" "one" "four" "one" "four" "one" "four"
[11] "one" "four" "one" "four" "one" "four" "one" "four" "one" "four"
$X2
[1] 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5
$X3
[1] 3 6 3 6 3 6 3 6 3 6 3 6 3 6 3 6 3 6 3 6
写入文本文件
write系列函数与read系列对应
与cat()函数类似,但不显示在屏幕上而是写入文件
数据框——write.table
csv格式——write.csv
> write.table (df,file='newfile.txt')
# quote
> write.table (df,file='newfile.txt',quote = FALSE)
# 行列名(若单列且无列名,默认为文件输入名)
> write.table (df,file='newfile.txt',row.names = FALSE,col.names = FALSE)
# sep
> write.table (df,file='newfile.txt',row.names = FALSE,col.names = FALSE,sep = ',')
# append
> write.table (df,file='newfile.txt',row.names = FALSE,col.names = FALSE,sep = ',',append = TRUE)
读写Excel文件
读取一个excel文件的最好方法是在excel中将其导出为一个逗号分隔文件(csv),并使用前文描述的方式将其导入到R中
也可以使用专门的R包
XLConnect包 读写文件
library(XLConnect)
# 读
#Two step Read Excel File
ex <- loadWorkbook ("data.xlsx")
edata <- readWorksheet(ex,1)
head(edata)
edata <- readWorksheet(ex,1,startRow=0,starCol=0,endRow=50,endCol=3)
#One step Read Excel File ###
readWorksheetFromFile ("data.xlsx",1,startRow=0,starCol=0,
endRow=50,endCol=3,header=TRUE)
# 写
#Four step Wtire Excel File
wb <- loadWorkbook("file.xlsx",create=TRUE)
createSheet(wb,"Sheet 1")
writeWorksheet(wb,data=mtcars,sheet = "Sheet 1")
saveWorkbook()
#One step Wtire Excel File ###
writeWorksheetToFile("file.xlsx",data = mtcars,sheet = "Sheet 1")
vignette("XLConnect")
xlsx包 读写文件
library(xlsx)
rdata <- read.xlsx("data.xlsx",sheetIndex=1,startRow = 1,endRow = 100)
write.xlsx(rdata,file = "rdata.xlsx",sheetName = "Sheet 1",append = F)
help(package="xlsx")
或者通过将excel数据赋值到剪贴板,在剪贴板读取
rio包
> library(rio)
> x <- import('clipboard', header=T)
> x
mpg cyl disp hp drat wt qsec vs am gear carb
1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
> class(x)
[1] "data.frame"
> str(x)
'data.frame': 9 obs. of 11 variables:
...
可以先试试这些包
不行的话还是通过csv导入