文章目錄
  1. 1. 内容提要
  2. 2. R的内置数据集
    1. 2.1. 查看本机安装的数据集
    2. 2.2. R的常用内置数据集
  3. 3. 文本文档的读取read.table
    1. 3.1. txt文件
    2. 3.2. read.table函数的参数
    3. 3.3. 使用read.table设定参数读取txt文档
    4. 3.4. 读取csv文件
    5. 3.5. 其他的read.table类函数
    6. 3.6. 写入函数write.table
  4. 4. 二进制文档的读取
    1. 4.1. 读取Excel文件
    2. 4.2. 读取其他常用统计软件的数据
    3. 4.3. Web数据
  5. 5. 脚本操作
    1. 5.1. 使用source函数执行R脚本
    2. 5.2. 使用sink重定向文本输出
    3. 5.3. cat
    4. 5.4. 重定向图像输出
    5. 5.5. 重定向图像输出例子
    6. 5.6. 批量重定向图像输出
    7. 5.7. 练习1
    8. 5.8. 练习1答案
    9. 5.9. 练习2

内容提要

  • R内置数据集
  • 文本文档: csv, txt
  • 二进制文件: Excel, SPSS, SAS
  • Web数据: api, 抓取网页
  • 其他: 数据库, XML

R的内置数据集

1
library(help = datasets)
1
head(airquality)
1
2
3
4
5
6
7
##   Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6

查看本机安装的数据集

1
data(package = .packages(TRUE))

R的常用内置数据集

1
tail(ToothGrowth)
1
2
3
4
5
6
7
##     len supp dose
## 55 24.8 OJ 2
## 56 30.9 OJ 2
## 57 26.4 OJ 2
## 58 27.3 OJ 2
## 59 29.4 OJ 2
## 60 23.0 OJ 2
1
head(USArrests)
1
2
3
4
5
6
7
##            Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
1
head(iris)
1
2
3
4
5
6
7
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
1
head(mtcars)
1
2
3
4
5
6
7
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

文本文档的读取read.table

txt文件

文件可点击此处下载 1.txt, 并存放到相应文件夹下

1
2
txt1 <- read.table("input_output\\1.txt")
txt1
1
2
3
4
5
##   V1       V2  V3
## 1 1 Zhejiang 50
## 2 2 Jiangsu 80
## 3 3 Shanghai 70
## 4 4 Beijing 100

read.table函数的参数

  • ?read.table
  • header = FALSE
  • sep = “”
  • col.names
  • nrows = -1
  • skip = 0
  • stringsAsFactors

使用read.table设定参数读取txt文档

文件可点击此处下载 2.txt, 并存放到相应文件夹下

1
2
txt2 <- read.table("input_output/2.txt", header = T, encoding = "UTF-8")
txt2
1
2
3
4
5
6
##   num city gender age height
## 1 1 北京 M 30 175
## 2 2 上海 F 25 165
## 3 3 广州 F 20 168
## 4 4 深圳 M 33 170
## 5 5 杭州 M 28 169

自己尝试其他参数: header, sep, col.names, nrows, skip, stringsAsFactors

读取csv文件

逗号分隔符文件, 可用记事本打开查看是否为逗号分隔.点此下载

1
2
csv <- read.csv("input_output\\csvtest.csv")
csv
1
2
3
4
5
6
##   num city gender age height weight
## 1 1 北京 M 30 175 70
## 2 2 上海 F 25 165 50
## 3 3 广州 F 20 168 49
## 4 4 深圳 M 33 170 68
## 5 5 杭州 M 28 169 65

注意默认参数: header = TRUE, sep = “,”

其他的read.table类函数

  • read.csv 分隔符为逗号
  • read.csv2 逗号作为小数位, 分号为分隔符
  • read.delim 句号为小数位, 制表符(Tab)为分隔符
  • read.delim2 逗号为小数位, 制表符(Tab)为分隔符
  • 万能公式: read.table

写入函数write.table

和read.table相反, 把数据写入到硬盘中的文件里, 且写入的文件也能再次用read.table读出

1
2
txt2$halfage <- txt2$age / 2
write.table(txt2, "halfage.txt")

相应的有write.csv, write.csv2……

二进制文档的读取

二进制数据格式:

  • 数据储存为二进制(0,1)
  • 格式比文本格式要小
  • 可读性差
  • 专有软件可以读取, 通用性差
  • 不推荐使用

读取Excel文件

  • 可以使用xlsx包中的read.xlsx函数
  • xlsx包的dependency有rJava包, 需要Java运行环境
  • 如果电脑无Java环境, 则无法成功加载xlsx包
  • 结论: 不建议使用xlsx包, 建议另存为csv文件

读取其他常用统计软件的数据

  • 其他常用统计软件: SAS, SPSS, Stata, Matlab等
  • 使用foreign包(SAS, SPSS, Stata)
  • 使用R.matlab包(Matlab)
  • 建议数据集均另存为csv文件
  • 还有很多不常用的数据格式可以用R读取, 请自行检索

Web数据

互联网上有大量数据, 可以使用R编程来导入数据

  • 拥有API接口的网站, 可以通过API接口下载数据到R中
  • 抓取网页数据: RCurl包, XML包, httr包等

数据清洗

脚本操作

使用source函数执行R脚本

使用方法:

1
source("script\\simple.R")
1
## [1] 38.5
  1. 注意工作空间地址及双斜杠
  2. 结果输出到显示器终端
  3. 和read.table结合使用, 封装数据和脚本

使用sink重定向文本输出

1
2
3
4
5
6
sink(file = "sink1.txt")
for(n in 1:30){
x <- 1:n
cat("Mean from 1 to", n, "is", mean(x),"\n", append = T)
}
sink()

cat

1
cat("I","Love","You")
1
## I Love You
1
cat("I","Love","You", sep = ",")
1
## I,Love,You
1
cat("Mean of 1:100 is", mean(1:100), "while sum is", sum(1:100))
1
## Mean of 1:100 is 50.5 while sum is 5050

重定向图像输出

  • sink()只能重定向文本输出, 图像输出应使用以下方法
  • pdf(“filename.pdf”)
  • png(“filename.png”)
  • jpeg(“filename.jpeg”)
  • bmp(“filename.bmp”)
  • 关闭方法 dev.off()

重定向图像输出例子

1
2
3
4
5
pdf("pic\\redirect.pdf")
plot(1:10,1:10,type = "n")
lines(1:10,1:10)
lines(1:10,10:1)
dev.off()

批量重定向图像输出

1
2
3
4
5
6
7
8
for(n in 1:10){
x <- 1:100
y <- x*(11 - n)/10
filename <- paste("pic",n,".png", sep = "")
png(filename)
plot(x,y, ylim = c(0,100))
dev.off()
}

练习1

编写脚本, 将0-360度, 每隔10度的正弦值写到一个文本文档中

1
2
3
4
5
6
7
8
The sin of 0 degree is 0 
The sin of 10 degree is 0.1736482
The sin of 20 degree is 0.3420201
The sin of 30 degree is 0.5
The sin of 40 degree is 0.6427876
...
The sin of 340 degree is -0.3420201
The sin of 350 degree is -0.1736482

练习1答案

1
2
3
4
5
sink("sin.txt")
for(n in 0:35){
cat("The sin of", 10*n, "degree is", sin(10*n/180*pi), "\n", append = T)
}
sink()

练习2

编写脚本, 画10张曲线图, 横轴是从-360°到360°, 纵轴是横轴的正弦值的1:10倍

1
2
3
4
5
6
7
8
x <- seq(-2*pi,2*pi,length.out = 100)
for(n in 1:10){
y <- n*sin(x)
filename <- paste("sin",n,".png", sep = "")
png(filename)
plot(x,y,type = "l", ylim = c(-11,11))
png()
}
文章目錄
  1. 1. 内容提要
  2. 2. R的内置数据集
    1. 2.1. 查看本机安装的数据集
    2. 2.2. R的常用内置数据集
  3. 3. 文本文档的读取read.table
    1. 3.1. txt文件
    2. 3.2. read.table函数的参数
    3. 3.3. 使用read.table设定参数读取txt文档
    4. 3.4. 读取csv文件
    5. 3.5. 其他的read.table类函数
    6. 3.6. 写入函数write.table
  4. 4. 二进制文档的读取
    1. 4.1. 读取Excel文件
    2. 4.2. 读取其他常用统计软件的数据
    3. 4.3. Web数据
  5. 5. 脚本操作
    1. 5.1. 使用source函数执行R脚本
    2. 5.2. 使用sink重定向文本输出
    3. 5.3. cat
    4. 5.4. 重定向图像输出
    5. 5.5. 重定向图像输出例子
    6. 5.6. 批量重定向图像输出
    7. 5.7. 练习1
    8. 5.8. 练习1答案
    9. 5.9. 练习2