1.1
#1_1
names<-c("低收入户","中等偏下户","中等收入户","中等偏上户","高收入户")
y08<-c(1500,2935,4203,5929,11290)
y09<-c(1549,3110,4502,6468,12319)
y10<-c(1870,3621,5222,7441,14050)
y11<-c(2001,4256,6208,8894,16783)
y12<-c(2316,4808,7041,10142,19009)
table1_1<-data.frame(指标=names,a=y08,b=y09,c=y10,d=y11,e=y12)
library(reshape)
rename(table1_1,c(指标="指标",a="2008年",b="2009年",c="2010年",d="2011年",e="2012年"))
save(table1_1,file="D:/R/R_text/table1_1.RData")
matrix1_1<-as.matrix(table1_1[,2:6])
dimnames(matrix1_1)<-list(c("低收入户","中等偏下户","中等收入户","中等偏上户","高收入户"),c("2008年","2009年","2010年","2011年","2012年"))
matrix1_1
save(matrix1_1,file="D:/R/R_text/matrix1_1.RData)
1.2
#1_2
load("C:/exercise/ch1/exercise1_2.RData")
table1_2<-exercise1_2
table1_2
#(1)升序
newdata1<-table1_2[order(table1_2$地区生产总值,decreasing=FALSE),]
newdata1
#(1)降序
newdata2<-table1_2[order(table1_2$地区生产总值,decreasing=TRUE),]
newdata2
#(2)重命名并存储
library(reshape)
table1_22<-rename(table1_2,c(地区生产总值="GDP"))
table1_22
save(table1_22,file="D:/R/R_text/table1_22.RData")
#(3)有放回随机抽6个
sample(table1_2$地区,6,replace=TRUE)
#(3)无放回随机抽6个
sample(table1_2$地区,6,replace=FALSE)
#(4)筛选-生产总值小于2万亿
sample(table1_2$地区[table1_2$地区生产总值<20000])
#(4)筛选-生产总值大于5万亿
sample(table1_2$地区[table1_2$地区生产总值>50000])
1.3
#1_3
#生成20个标准正态分布的随机数
a<-rnorm(20)
a
#4*5矩阵
matrix1_31<-matrix(data=a,nrow=4,ncol=5)
dimnames(matrix1_31)<-list(c(1,2,3,4),c(1,2,3,4,5))
matrix1_31
#5*4矩阵
matrix1_32<-matrix(data=a,nrow=5,ncol=4)
dimnames(matrix1_32)<-list(c(1,2,3,4,5),c(1,2,3,4))
matrix1_32