1、统计学基于R语言第二版CH1例题代码统计学基于R语言第二版Ch1例题代码第一章设置路径setwd(C:/Users/ch1/第1章 例题CSV格式数据)读入数据example_1-read.csv(example1_1.csv)文本框1-1x-8y-c(80,87,98,73,100)z-example_1文本框1-2sum(y)mean(y)var(y)barplot(y,xlab = 类别,ylab = 计数,col = c(1:5)文本框1-3help(mean)help(package=stats)文本框1-4install.packages(car)install.packages(
2、vcd)library(car)文本框1-5install.packages(c(agricolae,aplpack,BSDA,car,corrgram)install.packages(c(DescTools,e1071,fmsb,forecast,gmodels)install.packages(c(gplots,HH,Hmisc,lm.beta,lsr)install.packages(c(pastecs,plotrix,plyr,psych,reshape)install.packages(c(scatterplot3d,sm,TeachingDemos,vcd,vioplot)文本框
3、1-6names-c(张青松,王宇翔,田思宇,徐丽娜,张志杰,赵颖颖,王志强,宋丽媛,袁芳芳,张建国)stas-c(68,85,74,88,63,78,90,80,58,63)math-c(85,91,74,100,82,84,78,100,51,70)mark-c(84,63,61,49,89,51,59,53,79,91)mana-c(89,76,80,71,78,60,72,73,91,85)acco-c(86,66,69,66,80,60,66,70,85,82)class- c(统计学,数学,营销学,管理学,会计学)table1_1-data.frame(names,stas,mat
4、h,mark,mana,acco)colnames(table1_1)-c(学生姓名,class)#table1_1-data.frame(学生姓名=names,统计学=stas,数学=math,营销学=mark,管理学=mana,会计学=acco)table1_1matrix1_1-matrix(cbind(stas,math,mark,mana,acco),ncol = 5)dimnames(matrix1_1)-list(names,class)save(matrix1_1,file = matrix1_1.RData)matrix1_1文本框1-7load(mmatrix1_1.RDa
5、ta)文本框1-8read.csv(example1_1.csv)read.csv(example1_1.csv,header = F)文本框1-9#install.packages(excel)#library(xlsx)table1_1-read.csv(table1_1.csv)table1_1文本框1-10save(table1_1,file = table1_1.RData)write.csv(table1_1,file = table1_1.csv)文本框1-11load(table1_1.RData)table1_1head(table1_1,3)tail(table1_1)t(
6、table1_1)t文本框1-12newdata-table1_1order(table1_1$学生姓名),newdatanewdata1-table1_1order(table1_1$统计学,decreasing=TRUE),newdata1文本框1-13load(table1_1.RData)mean(table1_1$统计学)mean(table1_1,2)文本框1-14load(matrix1_1.RData)rowsum1-colSums(matrix1_1,1:5)rbind(matrix1_1,totals=rowsum1)colsum1-rowSums(matrix1_1)co
7、lsum1cbind(matrix1_1,totals=colsum1)matrix1_1mean(matrix1_1)apply(matrix1_1,1,sum)apply(matrix1_1,2,sum)apply(matrix1_1,1,mean)apply(matrix1_1,2,mean)apply(matrix1_1,2,sd)table1_1apply(table1_1,2:6,2,sum)apply(table1_1,2:6,2,mean)apply(table1_1,2:6,2,sd)apply(table1_1,-1,1:10,1,mean)文本框1-15load(tabl
8、e1_1.RData)library(reshape)table1_1rename(table1_1,c(学生姓名=姓名,统计学=统计)文本框1-16load(table1_1.RData)table1_1_1-edit(table1_1)save(table1_1_1,file=table1_1_1.RData)load(table1_1_1.RData)table1_1_1文本框1-17newdata-data.frame(性别=character(0),年龄=numeric(0)newdata-edit(newdata)save(newdata,file = newdata.RData)
9、load(newdata.RData)newdata文本框1-18x-c(2,4,6,NA)is.na(x)文本框1-19x-c(2,4,6,NA)sum(x)sum(x,na.rm = T)文本框1-20table1_1_2-read.csv(table1_1_2.csv)save(table1_1_2,file = table1_1_2.RData)load(table1_1_2.RData)table1_1_2table1_1_3-na.omit(table1_1_2)table1_1_3save(table1_1_3,file = table1_1_3.RData)文本框1-21loa
10、d(table1_1.RData)table1_1matrix1_1-as.matrix(table1_1,2:6)matrix1_1rownames(matrix1_1)-table1_1,1matrix1_1table1-as.data.frame(matrix1_1)table1文本框1-22table1_1as.vector(table1_1$统计学)as.vector(as.matrix(table1_1,2:6)文本框1-23rnorm(10)set.seed(15)rnorm(10,50,5)runif(10,0,100)rexp(10)rchisq(10,15)set.seed
11、(1000)rnorm(10)文本框1-24example_1-read.csv(example1_1.csv)save(example_1,file = example1_1.RData)load(example1_1.RData)example_1sample(example_1$姓名,10,replace = F)sample(example_1$姓名,100,replace = T)sample(example_1$姓名example_1$考试分数90)文本框1-25文本框1-26load(example1_1.RData)example_1x-example_1,4xmyfun-fu
12、nction(x) n-length(x) mean-sum(x)/n median-median(x) r-max(x)-min(x) s-sd(x) summ-data.frame(c(mean,median,r,s) rownames(summ)=c(平均数,中位数,极差,标准差) names(summ)-值 return(summ)myfun(x)文本框1-27layout(matrix(c(1,2,3,3),nrow = 2,ncol = 2),widths = c(2,1)par(mai=c(0.6,0.6,0.1,0.1),cex=0.7)x-rnorm(5000)y-rchis
13、q(5000,10)hist(x,prob=T,col = lightblue,xlab=x,ylab = Density,ylim = c(0,0.4),main=)hist(y,freq = F,col = pink,xlab = y,ylab = Density,main = )boxplot(x,col=red,lwd=1)文本框1-28layout(matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = T),heights = c(2,1)par(mai=c(0.6,0.6,0.1,0.1),cex=0.7)x-rnorm(1000)y-rchisq(1000,10)hist(x,prob=T,col = lightblue,xlab = x,ylab = Density,ylim = c(0,0.4),main = )plot(x,y,xlab = x,ylab = y)boxplot(y,col = red,lwd=1,horizontal=T,varwidth =T)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1