文章目录
1.连接键盘与显示器
1.1使用scan()函数
scan()从文件中读取或者用键盘输入一个向量
> scan("z1.txt")
Read 4 items
[1] 123 4 5 6
> scan("z2.txt")
Read 4 items
[1] 123.0 4.2 5.0 6.0
> scan("z3.txt")
Error in scan("z3.txt") : scan()需要'a real', 而不是'abc‘
#默认double模式,因文件内容不是数值,出现错误。
> scan("z3.txt",what=" ")
Read 4 items
[1]"abc" "de" "f" "g"
> scan("z4.txt",what=" ")
Read 4 items
[1] "abc" "123" "6" "y“
#加上参数what=" ",表明想用字符模式
scan()默认以“空白字符”分隔各项。空白字符包括空格、回车/换行符、水平制表符。如果是其他分隔符,可用可选参数sep设置其他分隔符
> x1 <- scan("z3.txt",what="")
Read 4 items
> x2 <- scan("z3.txt",what="",sep="\n")
Read 3 items
> x1
[1] "abc" "de" "f" "g"
> x2
[1] "abc" "de f" "g"
> x1[2]
[1] "de"
> x2[2]
[1] "de f"
利用scan()函数从键盘读取数据。键入空行表示结束输入。
> v<-scan("")
1: 1 4 5
4: 5 6 7
7: 8 9
9:
Read 8 items
> v <- scan("",quiet = TRUE)
1: 1 4 5
4: 5 6 7
7: 8 9
9:
>
1.2使用readline()函数
从键盘输入单行数据,用readline()函数会非常方便。
> a <- readline()
123
> a
[1] "123"
> a <- readline()
abhdb1221
> a
[1] "abhdb1221"
> a <- readline()
abc def g
> a
[1] "abc def g"
> xm <- readline("type your names: ")
type your names: tiancai
> xm
[1] "tiancai"
1.3输出到显示器
cat()会比print()好用一点。cat()输出的内容默认以空格分隔。或者利用sep参数设置为其他分隔符。
> x <- 1:3
> cat(x,"abc","de\n")
1 2 3 abc de
# cat()输出的内容默认以空格分隔
# /n表示换行符
> cat(x,"abc","de\n",sep="")
123abcde
> cat(x,"abc","de\n",sep="\n")
1
2
3
abc
de
> x <- c(5,12,13,8,88)
> cat(x,sep=c(".",".",".","\n","\n"))
5.12.13.8
88
2.读写文件
2.1从文件中读取数据框或矩阵
懒得弄文件了,看老师的代码吧!
> setwd("D:\\jzamu\\tmp\\amupro\\rpro")
> z <- read.table("z.txt",header=TRUE)
> z
name age
1 John 25
2 Mary 28
3 Jim 19
> class(z)
[1] "data.frame"
注意,此处scan()不能正确读取数据框,因为这个文件中数值和字符混杂(还含有表头)。似乎没有办法直接从文件中读取矩阵,不过借助其他工具可以轻松办到。一个简单快速的办法是用scan()逐行读取。用matrix()中的byrow选项设定矩阵是按行存储,而不是按列存储。
> setwd("D:\\jzamu\\tmp\\amupro\\rpro")
> x <- matrix(scan("x.txt"),nrow=3,byrow=TRUE)
Read 9 items
> x
[,1] [,2] [,3]
[1,] 1 0 1
[2,] 1 1 1
[3,] 1 1 0
> class(x)
[1] "matrix" "array"
我们可以自定义函数:
# 自定义函数
read.matrix <- function(filename) {
as.matrix(read.table(filename))
}
> setwd("D:\\jzamu\\tmp\\amupro\\rpro")
> v<-read.matrix("z.txt")
> v
V1 V2
[1,] "name" "age"
[2,] "John" "25"
[3,] "Mary" "28"
[4,] "Jim" "19"
> class(v)
[1] "matrix" "array"
2.2读取文本文件
可以使用readLines()读取文本文件,无论是每次一行还是一次性全部读取。
z.txt
name age
John 25
Mary 28
Jim 19
> v <- readLines("z.txt")
> v
[1] "name age" "John 25" "Mary 28" "Jim 19"
> class(v) #字符串向量
[1] "character"
利用readLines()函数逐行读取文件中的内容
> v <- file("z.txt","r")
> readLines(v,n=1)
[1] "name age"
> readLines(v,n=1)
[1] "John 25"
> readLines(v,n=1)
[1] "Mary 28"
> readLines(v,n=1)
[1] "Jim 19"
> readLines(v,n=1)
character(0)
在R中,利用seek()函数实现“倒带”功能:从文件开始处重新读取文件。
> v <- file("z.txt","r")
> readLines(v,n=2)#注意此处
[1] "name age" "John 25"
> seek(con=v,where=0)
[1] 36
> readLines(v,n=1)
[1] "name age"
where=0参数表示从文件开头读文件
2.3读取Excel表格文件
- 【方法一】
1.复制Excel文件中的数据
2.在R语言环境下运行命令:read.table(“clipboard”,header=TRUE) - 【方法二】
1.在Rstudio环境下设置国内镜像:Tools>global options>packages
2.安装运行读取Excel文件的包readxl:
install.packages(“readxl”)
library(“readxl”)
read_excel(“C:\test\me\tmp.xls”,sheet=1) 或者 read_excel(“C:/test/me/tmp.xls”)
2.4读取csv文件
read.csv(“tmp.csv",header=TRUE)
2.5通过URL在远程计算机上访问文件
某些I/O函数,如read.table()和scan(),可以用网站地址作为参数。
例如,我们读取美国加州大学欧文分校的一些数据, 网址为http://archive.ics.uci.edu/ml/datasets.html,使用其中的Echocardiogram数据集。访问链接浏览页面后我们找到文件的位置,接着用R读取数据。代码如下:
> uci <- "http://archive.ics.uci.edu/ml/machine-learning-databases/"
> uci <- paste(uci,"echocardiogram/echocardiogram.data",sep="")
> ecc <- read.csv(uci)
2.6R自带数据集的读取
> data() #查看自带的数据集
> force(iris) #查看R自带数据集iris的内容
2.7写文件
由于R语言主要用于统计功能,读文件可能比写文件更为常用。但是写文件有时候也很必要,下面就介绍一些写文件的方法。
- write.table()函数的用法与 read.table()非常相似,只不过它把数据框写入文件而不是从文件中读取。
> kids <- c("Jack","Jill")
> ages <- c(12,10)
> #d<- data.frame(kids=c("jack","ji"),ages=c(10,18))
#d
> d <- data.frame(kids,ages)
> d
kids ages
1 Jack 12
2 Jill 10
> write.table(d,"kds.txt")
# 文件kds.txt的内容如下:
"kids" "ages"
"1" "Jack" 12
"2" "Jill" 10
函数cat()可以用来写入文件,一次写入一部分。
> cat("abc\n",file="u.txt")
> cat("de\n",file="u.txt",append=TRUE)
#文件u.txt内容
abc
de
用函数cat() 写入一行
> cat(file="v.txt",1,2,3,"abc","xyz\n")
#文件v.txt内容
1 2 3 abc xyz
用函数writeLines() 写入文件。
> v <- file(“www.txt”,“w”) #写
> writeLines(c("abc","de","f"),v)
> close(v) #主动关闭
#文件www.txt内容
abc
de
f
3.获取文件和目录信息
setwd("")
> ?files
到此结束!!!!!!!!!