1第二章答案:Ex2.1x-c(1,2,3)y-c(4,5,6)e-c(1,1,1)z=2*x+y+ez1=crossprod(x,y)#z1为x1与x2的内积或者x%*%yz2=tcrossprod(x,y)#z1为x1与x2的外积或者x%o%yz;z1;z2要点:基本的列表赋值方法,内积和外积概念。内积为标量,外积为矩阵。Ex2.2A-matrix(1:20,c(4,5));AB-matrix(1:20,nrow=4,byrow=TRUE);BC=A+B;C#不存在AB这种写法E=A*B;EF-A[1:3,1:3];FH-matrix(c(1,2,4,5),nrow=1);H#H起过渡作用,不规则的数组下标G-B[,H];G要点:矩阵赋值方法。默认是byrow=FALSE,数据按列放置。取出部分数据的方法。可以用数组作为数组的下标取出数组元素。2Ex2.3x-c(rep(1,times=5),rep(2,times=3),rep(3,times=4),rep(4,times=2));x#或者省略times=,如下面的形式x-c(rep(1,5),rep(2,3),rep(3,4),rep(4,2));x要点:rep()的使用方法。rep(a,b)即将a重复b次Ex2.4n-5;H-array(0,dim=c(n,n))for(iin1:n){for(jin1:n){H[i,j]-1/(i+j-1)}};HG-solve(H);G#求H的逆矩阵ev-eigen(H);ev#求H的特征值和特征向量要点:数组初始化;for循环的使用待解决:如何将很长的命令(如for循环)用几行打出来再执行?每次想换行的时候一按回车就执行了还没打完的命令...Ex2.5StudentData-data.frame(name=c(zhangsan,lisi,wangwu,zhaoliu,dingyi),sex=c(F,M,F,M,F),age=c(14,15,16,14,15),height=c(156,165,157,162,159),weight=c(42,49,41.5,52,45.5));StudentData要点:数据框的使用待解决:SSH登陆linux服务器中文显示乱码。此处用英文代替。Ex2.6write.table(StudentData,file=studentdata.txt)#把数据框StudentData在工作目录里输出,输出的文件名为studentdata.txt.3StudentData_a-read.table(studentdata.txt);StudentData_a#以数据框的形式读取文档studentdata.txt,存入数据框StudentData_a中。write.csv(StudentData_a,studentdata.csv)#把数据框StudentData_a在工作目录里输出,输出的文件名为studentdata.csv,可用Excel打开.要点:读写文件。read.table(file)write.table(Rdata,file)read.csv(file)write.csv(Rdata,file)外部文件,不论是待读入或是要写出的,命令中都得加双引号。Ex2.7Fun-function(n){if(n=0)list(fail=pleaseinputaintegerabove0!)else{repeat{if(n==1)breakelseif(n%%2==0){n-n/2}elsen-3*n+1}list(sucess!)}4}在linux下新建一个R文件,输入上述代码,保存为2.7.R然后在当前目录下进入R环境,输入source(2.7.R),即打开了这个程序脚本。然后就可以执行函数了。输入Fun(67),显示sucess!输入Fun(-1),显示$fail[1]pleaseinputaintegerabove0!待解决:source(*.R)是可以理解为载入这个R文件吧?如何在R环境下关闭R文件呢?Ex3.1新建txt文件如下:3.1.txt74.379.575.073.575.874.073.567.275.873.578.875.673.575.075.872.079.576.573.579.568.875.078.872.068.876.573.572.775.070.478.078.874.364.376.574.374.770.472.776.570.472.075.875.870.476.565.077.273.572.780.572.065.080.371.277.676.568.873.577.280.572.074.369.781.267.381.667.372.784.369.774.371.274.375.072.075.467.381.675.071.271.269.773.570.475.072.767.370.376.573.572.068.073.568.074.372.772.774.370.4编写一个函数(程序名为data_outline.R)描述样本的各种描述性统计量。data_outline-function(x){5n-length(x)m-mean(x)v-var(x)s-sd(x)me-median(x)cv-100*s/mcss-sum((x-m)^2)uss-sum(x^2)R-max(x)-min(x)R1-quantile(x,3/4)-quantile(x,1/4)sm-s/sqrt(n)g1-n/((n-1)*(n-2))*sum((x-m)^3)/s^3g2-((n*(n+1))/((n-1)*(n-2)*(n-3))*sum((x-m)^4)/s^4-(3*(n-1)^2)/((n-2)*(n-3)))data.frame(N=n,Mean=m,Var=v,std_dev=s,Median=me,std_mean=sm,CV=cv,CSS=css,USS=uss,R=R,R1=R1,Skewness=g1,Kurtosis=g2,row.names=1)}进入R,source(data_outline.R)#将程序调入内存serumdata-scan(3.1.txt);serumdata#将数据读入向量serumdata。data_outline(serumdata)结果如下:6NMeanVarstd_devMedianstd_meanCVCSSUSSR110073.69615.416753.92641773.50.39264175.3278571526.258544636.320R1SkewnessKurtosis14.60.038542490.07051809要点:read.table()用于读表格形式的文件。上述形式的数据由于第七行缺几个数据,故用read.table()不能读入。scan()可以直接读纯文本文件。scan()和matrix()连用还可以将数据存放成矩阵形式。X-matrix(scan(3.1.txt,0),ncol=10,byrow=TRUE)#将上述数据放置成10*10的矩阵。scan()还可以从屏幕上直接输入数据。Y-scan()然后按提示输入即可。结束输入时按回车即可。Ex3.2hist(serumdata,freq=FALSE,col=purple,border=red,density=3,angle=60,main=paste(thehistogramofserumdata),xlab=age,ylab=frequency)#直方图。col是填充颜色。默认空白。border是边框的颜色,默认前景色。density是在图上画条纹阴影,默认不画。angle是条纹阴影的倾斜角度(逆时针方向),默认45度。main,xlab,ylab是标题,x和y坐标轴名称。lines(density(serumdata),col=blue)#密度估计曲线。7x-64:85lines(x,dnorm(x,mean(serumdata),sd(serumdata)),col=green)#正态分布的概率密度曲线plot(ecdf(serumdata),verticals=TRUE,do.p=FALSE)#绘制经验分布图lines(x,pnorm(x,mean(serumdata),sd(serumdata)),col=blue)#正态经验分布qqnorm(serumdata,col=purple)#绘制QQ图qqline(serumdata,col=red)#绘制QQ直线Ex3.3stem(serumdata,scale=1)#作茎叶图。原始数据小数点后数值四舍五入。Thedecimalpointisatthe|64|30066|2333368|0088877770|3444444222272|000000077777775555555555574|03333333370000000468888876|555555522678|088855580|35526682|884|3boxplot(serumdata,col=lightblue,notch=T)#作箱线图。notch表示带有缺口。fivenum(serumdata)#五数总结[1]64.371.273.575.884.3Ex3.4shapiro.test(serumdata)#正态性Shapori-Wilk检验方法Shapiro-Wilknormalitytestdata:serumdataW=0.9897,p-value=0.6437结论:p值0.05,可认为来自正态分布的总体。ks.test(serumdata,pnorm,mean(serumdata),sd(serumdata))#Kolmogrov-Smirnov检验,正态性One-sampleKolmogorov-Smirnovtestdata:serumdataD=0.0701,p-value=0.7097alternativehypothesis:two-sidedWarningmessage:Inks.test(serumdata,pnorm,mean(serumdata),sd(serumdata)):9cannotcomputecorrectp-valueswithties结论:p值0.05,可认为来自正态分布的总体。注意,这里的警告信息,是因为数据中有重复的数值,ks检验要求待检数据时连续的,不允许重复值。Ex3.5y-c(2,4,3,2,4,7,7,2,2,5,4,5,6,8,5,10,7,12,12,6,6,7,11,6,6,7,9,5,5,10,6,3,10)#输入数据f-factor(c(rep(1,11),rep(2,10),rep(3,12)))#因子分类plot(f,y,col=lightgreen)#plot()生成箱线图x-c(2,4,3,2,4,7,7,2,2,5,4)y-c(5,6,8,5,10,7,12,12,6,6)z-c(7,11,6,6,7,9,5,5,10,6,3,10)boxplot(x,y,z,names=c(1,2,3),col=c(5,6,7))#boxplot()生成箱线图结论:第2和第3组没有显著差异。第1组合其他两组有显著差异。Ex3.6数据太多,懒得录入。离散图应该用plot即可。Ex3.7studata-read.table(3.7.txt)#读入数据data.frame(studata)#转化为数据框V1V2V3V4V5V611alicef1356.584.022beckaf1365.398.01033gailf1464.390.044karenf1256.377.055kathyf125