Lecture 1
1 參考書籍
- R Cookbook:http://www.cookbook-r.com/
- R in Action 《R語言實戰(zhàn)》
- 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 生成隨機數(shù)
random number: a = runif(3,min=0,max=100)
rounding od numbers: 去小數(shù)位取整 floor(a), 去小數(shù)位且整數(shù)進一 ceiling(a), 對數(shù)組保留小數(shù)位 round(a,4)
random numbers from other distributions: 正態(tài)分布 rnorm(), 指數(shù)分布 rexp(), 二項分布 rbinom(), 幾何分布 rgeom(), 負二項分布 rnbinom(), etc.
repeatable random numbers: set.seed()
3.3 data input 輸入數(shù)據(jù)
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()