感谢: 吴明昊老师 Swirl包作者 《Elegant Graphics for Dataanlysis》书作者 《R Graphics Cookbook》书作者
第一课 R初体验(12 h)
1、安装R
2、安装RStudio
3.在RStudio下输入以下命令:
install.packages("swirl")
安装Swirl
4、调用Swirl:
library(swirl)swirl()install_from_swirl("R Programming")
然后就可以开始使用Swirl了。
theme_set(theme_bw())
R工作目录
file.create("mytest.R")
list.files()
unlink('testdir2',recursive= TRUE)
加载数据
R中数组
c(1,1,2,4,5,7)
attach(mpg)
save(baochunsm,"wenjianming")
ggsave("*.png",width =5,height=5)
R中向量
paste(my_char,collapse =" ")
plotmath
R中缺失值处理
NA
NaN
To generate NaN,try dividing (using a forward slash) 0 by 0 .
R构造子集向量
矩阵和数据结构
my_matrix2<- matrix(1:20,4,5)
cbind(patients,my_matrix)
colnames(my_data) <- cnames
R中逻辑表达式
all
any
R中功能
R中申请和供给
lapply
sapply
vapply and tapply
flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))
模拟
仿真
3D图包 (latticeExtra)
第二课 R基本语法
youtube学习
ggplot2:Elegant Graphics for Dataanlysis
http://www.cookbook-r.com/Graphs/
http://www.datavizcatalogue.com
#Referrence Book
#ggplot2: Elegant Graphics for Data Analysis (Use R!):
#http://www.amazon.com/ggplot2-Elegant-Graphics-Data-Analysis/dp/0387981403
#R Graphics Cookbook:
#http://www.cookbook-r.com/Graphs/
#http://www.amazon.com/R-Graphics-Cookbook-Winston-Chang/dp/1449316956
#install.packages("ggplot2")
library(ggplot2)
#Scatter Plot
plot(mtcars$wt, mtcars$mpg)
#Line Graph
plot(pressure$temperature, pressure$pressure, type="l")
points(pressure$temperature, pressure$pressure) #add points
lines(pressure$temperature, pressure$pressure/2, type="l",col="red")
points(pressure$temperature, pressure$pressure/2,col="red")
#Bar Graph
barplot(table(mtcars$cyl))
#Histogram
hist(mtcars$mpg, breaks=10)
#Box Plot
boxplot(mtcars$mpg)
#Plotting a Function Curve
curve(x^3 - 5*x, from=-4, to=4)
myfun <- function(xvar) {
1/(1 + exp(-xvar + 10))
}
curve(myfun(x), from=0, to=20)
#Basic use
dat=diamonds
qplot(carat, price, data = diamonds)
qplot(log(carat), log(price), data = diamonds)
qplot(carat, x * y * z, data = diamonds)
#Colour, size,