Lecture 1
1 参考书籍
- R Cookbook:http://www.cookbook-r.com/
- R in Action 《R语言实战》
- ggplot2:Elegant Graphics for Data Analysis(Use R!)
- Advanced R(高级使用者)
2 Install and loading Rpackages
install.packages('ggplot2')
library(ggplot2)
updata.packages()
3 R language basics
3.1 vector 向量
Create a vector: v = c(1,4,4,3,2,2,3) or w = c("apple","banana")
Return certain elements: v[c(2,3,4)] or v[2:4] or v[c(2,4,3)]
Delete certain element: v = v[-2] or v = v[-2:-4]
Extract elements: v[v<3]
Find elements:which(v==3) *Note:the returns are the indicates of elements
which.max(v), which.min(v)
3.2 number 生成随机数
random number: a = runif(3,min=0,max=100)
rounding od numbers: 去小数位取整 floor(a), 去小数位且整数进一 ceiling(a), 对数组保留小数位 round(a,4)
random numbers from other distributions: 正态分布 rnorm(), 指数分布 rexp(), 二项分布 rbinom(), 几何分布 rgeom(), 负二项分布 rnbinom(), etc.
repeatable random numbers: set.seed()
3.3 data input 输入数据
loading local data: read.csv(file="/documents/file.txt) or
read.table(file="/documents/file.txt)
loading online data: read.csv("http://www.macalester.edu/kaplan/ISM/datasets/swim100m.csv")
Attach: attach() 将第一列作为变量名使用
3.4 Graphs 画图
plot: plot()
直方图 histograms: hist()
密度图 density plot: plot(density())
散点图 scatter plot plot()
箱式图 box plot : boxplot()
q plot: qqnorm(), qqline(), qqplot()