emmm,那就是学完这本之后,要看sql,学习一下数据库了。实习要求的东西,也得着手了。内心还是很虚,不想动手,又得硬着头皮前行,晚上学习是不是不大好,大脑总是很兴奋,睡不着。昨晚做了一个很神奇的梦,老爸变得好温柔,简直难以置信,老妈还是个捣蛋鬼,写我的字帖,跟个小孩似的,根本不按字帖来。
###R的对象们
> a <- matrix(c('a',1,2,3),nrow = 2)
> a
[,1] [,2]
[1,] "a" "2"
[2,] "1" "3"
> is.matrix(a)
[1] TRUE
> is.character(a)
[1] TRUE
> is.numeric(a[2,2])
[1] FALSE
练习8.1.读入数据
write.csv(WorldPhones,file = 'E:/R/R lab/学R/r4r/wp.csv')
#读入文件
myfile <- file.choose()
wp <- read.csv(file = myfile,header = T)
summary(wp)
plot(wp)
#对数据进行作图
phones <- unlist(wp[,2:8])
contry <- rep(names(wp)[2:8], each = length(wp$X))
year <- rep(wp$X,7)
boxplot(phones~contry)
boxplot(phones~year)
#将年份列的列名称改为‘year’
names(wp)[1] <- 'year'
#将行名改为对应年份
rownames(wp) <- wp$year
##数据类型
str(wp)
wp$year <- as.numeric(wp$year)
decade <- ifelse(wp$year >= 1950 & wp$year <= 1959,'1950s' , '1960s')
wp$decade <- decade
str(wp)
练习8.3.矩阵。
matrix(data = 1:30,nrow = 5,ncol = 6)
练习8.4.矩阵与数据框。
#选择2,4,5行
wp[c(2,4,5),]
wp_mt[c(2,4,5),]
反选2,4,5行
wp[-c(2,4,5),]
wp_mt[-c(2,4,5),]
wp_mt <- as.matrix(wp)
#转为矩阵,矩阵内所有的元素都变成字符形式,而数据框允许不同类型的变量存在
str(wp_mt)
str(wp)
#选择1956年欧洲电话数量
wp['1956',"Europe"]
wp_mt['1956','Europe']
#选择欧洲和亚洲两列
wp[,c('Europe','Asia')]
wp_mt[,c('Europe','Asia')]
#选择2,4,5行
wp[c(2,4,5),]
wp_mt[c(2,4,5),]
#反选2,4,5行
wp[-c(2,4,5),]
wp_mt[-c(2,4,5),]
练习8.5.计算。
annualsum <- rowSums(wp[,2:8])
annualdiff <- diff(annualsum)
annualdiffeach <- apply(wp[,2:8], MARGIN = 2, diff)
annualrate <- annualdiff/annualsum[1:6]
annualrateeach <- annualdiffeach/(wp[c(1:6),2:8])
#等同于
annualrateeach <- apply(wp[,2:8],MARGIN = 2,
function(x) diff(x)/x[1:6])
练习8.6.作图。
请在同-张图上以合理的布局、颜色和线型,做出亚洲1951到1961年之间:
小tips:如何将两个变量(不同数量级量纲)作到同一个图中
par(mfrow=c(2,1),mar=c(0,4,3,1))
plot(wp$year,wp$Asia,type = 'l',xlim = c(1951,1961),
axes = FALSE,xlab = '',ylab = 'Phone Asia')
axis(2)
box()
par(mar=c(4,4,0,1))
plot(wp$year[1:6],annualrateeach[,'Asia'],
type = 'l',xlim = c(1951,1961),
xlab = 'year',ylab = 'Increase rate')
dev.off()
小tips:
par(mfrow=c(1,1))
mylim <- range(wp[,2:8])
mycol <- rainbow(7)
plot(wp$year,wp[,2],col=mycol[1],type = 'l',
xlab = 'year',ylab = 'Phone number',ylim = mylim)
for (i in 3:8) {
lines(wp$year, wp[,i], col=mycol[i-1])
legend('topleft',legend = names(wp)[2:8],lty = 1,
col = mycol,bty = 'n')
}
par(mfrow=c(3,3),mar=c(4,4,0.1,0.1))
for (i in 2:8) {
plot(wp$year,wp[,i],col=mycol[i-1],
xlab = 'year',ylab = names(wp)[i])
m <- lm(wp[,i]~wp$year)
a1 <- m$coefficients[1]
a2 <- m$coefficients[2]
abline(m)
legend('topleft',bty = 'n',
as.expression(substitute(y==a+b*x,
list(a=a1,b=a2))))
}