R语言习题.docx
- 文档编号:2980731
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:17
- 大小:263.56KB
R语言习题.docx
《R语言习题.docx》由会员分享,可在线阅读,更多相关《R语言习题.docx(17页珍藏版)》请在冰点文库上搜索。
R语言习题
R语言习题
一组学生参加了数学、科学和英语考试。
为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来。
另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推。
最后,希望按字母顺序对学生排序。
Excel中的数据表
StuId
StuName
Math
Science
English
1
JohnDavis
502
95
25
2
AngelaWilliams
465
67
12
3
BullJones
621
78
22
4
CherylCushing
575
66
18
5
ReuvenYtzrhak
454
96
15
6
JoelKnox
634
89
30
7
MaryRayburn
576
78
37
8
GregEngland
421
56
12
9
BradTmac
599
68
22
10
TracyMcgrady
666
100
38
1:
输入数据——R语言导入xlsx
>install.packages("xlsx")
>library(xlsx)
>workbook<-"D:
/R/StuScore.xlsx"
MathScienceEnglish
84.715.19.5
3:
通过函数mean()来计算各行的均值以及获得综合得分,并使用cbind()将其添加到花名册中
>#3在afterscale中计算标准差均值,并将其添加到StuScore
>score<-apply(afterscale,1,mean)#1表示行,mean表示均值函数
>StuScore<-cbind(StuScore,score)
>StuScore
StuIdStuNameMathScienceEnglishscore
11JohnDavis50295250.22
22AngelaWilliams4656712-1.00
33BullJones62178220.21
44CherylCushing5756618-0.38
55ReuvenYtzrhak4549615-0.30
66JoelKnox63489300.78
77MaryRayburn57678370.56
88GregEngland4215612-1.42
99BradTmac5996822-0.10
1010TracyMcgrady666100381.43
4:
函数quantile()给出学生综合得分的百分位数
quantile(x,probs):
求分位数,其中x为待求分位数的数值型向量,probs为一个由[0,1]之间的概率值组成的数值向量
>afterquantile<-quantile(score,c(.8,.6,.4,.2))
>afterquantile
80%60%40%20%
0.600.21-0.18-0.50
5:
使用逻辑运算符,把score转为等级(离散型)
>StuScore$grade[score>=afterquantile[1]]<-"A"
>StuScore$grade[score
>StuScore$grade[score
>StuScore$grade[score
>StuScore$grade[score >StuScore StuIdStuNameMathScienceEnglishscoregrade 11JohnDavis50295250.22B 22AngelaWilliams4656712-1.00E 33BullJones62178220.21B 44CherylCushing5756618-0.38E 55ReuvenYtzrhak4549615-0.30E 66JoelKnox63489300.78B 77MaryRayburn57678370.56B 88GregEngland4215612-1.42E 99BradTmac5996822-0.10E 1010TracyMcgrady666100381.43B 6: 使用strsplit()以空格为界把学生姓名拆分为姓氏和名字 >StuScore$StuName<-as.character(StuScore$StuName) >is.character(StuScore$StuName) [1]TRUE >name<-strsplit(StuScore$StuName,"") >name [[1]] [1]"John""Davis" [[2]] [1]"Angela""Williams" [[3]] [1]"Bull""Jones" [[4]] [1]"Cheryl""Cushing" [[5]] [1]"Reuven""Ytzrhak" [[6]] [1]"Joel""Knox" [[7]] [1]"Mary""Rayburn" [[8]] [1]"Greg""England" [[9]] [1]"Brad""Tmac" [[10]] [1]"Tracy""Mcgrady" 7: 把name分成Firstname和LastName,加入到StuScore中 >FirstName<-sapply(name,"[",1) >LastName<-sapply(name,"[",2) >StuScore<-cbind(FirstName,LastName,StuScore[,-1]) >StuScore FirstNameLastNameLastNameStuNameMathScienceEnglishscoregrade 1JohnDavisDavisJohnDavis50295250.22B 2AngelaWilliamsWilliamsAngelaWilliams4656712-1.00E 3BullJonesJonesBullJones62178220.21B 4CherylCushingCushingCherylCushing5756618-0.38E 5ReuvenYtzrhakYtzrhakReuvenYtzrhak4549615-0.30E 6JoelKnoxKnoxJoelKnox63489300.78B 7MaryRayburnRayburnMaryRayburn57678370.56B 8GregEnglandEnglandGregEngland4215612-1.42E 9BradTmacTmacBradTmac5996822-0.10E 10TracyMcgradyMcgradyTracyMcgrady666100381.43B 8: order排序 >StuScore[order(LastName,FirstName),] FirstNameLastNameLastNameStuNameMathScienceEnglishscoregrade 4CherylCushingCushingCherylCushing5756618-0.38E 1JohnDavisDavisJohnDavis50295250.22B 8GregEnglandEnglandGregEngland4215612-1.42E 3BullJonesJonesBullJones62178220.21B 6JoelKnoxKnoxJoelKnox63489300.78B 10TracyMcgradyMcgradyTracyMcgrady666100381.43B 7MaryRayburnRayburnMaryRayburn57678370.56B 9BradTmacTmacBradTmac5996822-0.10E 2AngelaWilliamsWilliamsAngelaWilliams4656712-1.00E 5ReuvenYtzrhakYtzrhakReuvenYtzrhak4549615-0.30E 9: 为StuScore绘制分组条形图 install.packages("vcd") library(vcd) fill_colors<-c()#不同的等级,不同的颜色显示 for(iin1: length(StuScore$Science)) { if(StuScore$Science[i]==100) { fill_colors<-c(fill_colors,"red") } elseif(StuScore$Science[i]<100&&StuScore$Science[i]>=80) { fill_colors<-c(fill_colors,"yellow") } elseif(StuScore$Science[i]<80&&StuScore$Science[i]>=60) { fill_colors<-c(fill_colors,"blue") }else{ fill_colors<-c(fill_colors,"green") } } barplot(StuScore$Science,#条形图 main="ScienceScore", xlab="Name",ylab="ScienceScore", col=fill_colors, names.arg=(paste(substr(FirstName,1,1),".",LastName)),#设定横坐标名称 border=NA,#条形框不设置边界线 font.main=4, font.lab=3, beside=TRUE) legend(x=8.8,y=100,#左上角点的坐标 cex=.8,#缩放比例 inset=5, c("Excellent","Good","Ordinary","Bad"), pch=c(15,16,17,19),#图例中的符号 col=c("red","yellow","blue","green"), bg="#821122",#背景色 xpd=TRUE,#可以在绘图区之外显示 text.font=8, text.width=.8, text.col=c("red","yellow","blue","green") 10: 现有6名患者的身高和体重,检验体重除以身高的平方是否等于22.5. 编号 1 2 3 4 5 6 身高m 1.75 1.80 1.65 1.90 1.74 1.91 体重kg 60 72 57 90 95 72 height<-c(1.75,1.80,1.65,1.90,1.74,1.91) weight<-c(60,72,57,90,95,72) sq.height<-height^2 ratio<-weight/sq.height t.test(ratio,mu=22.5)#t检验 11: 将三种不同菌型的伤寒病毒a,b,c分别接种于100,9,11只小白鼠上,观察其存活天数,问三种菌型下小白鼠的平均存活天数是否有显著差异。 a菌株: 2,4,3,2,4,7,7,2,5,4 b菌株: 5,6,8,5,10,7,12,6,6 c菌株: 7,11,6,6,7,9,5,10,6,3,10 准备数据表,day和type各位一列。 #数据读取,将test.txt中的内容保存到bac中,header=T表示保留标题行。 bac<-read.table(“D: /anova.data.txt”,header=T) #将ba数据框中的type转换为因子(factor) bac$type<-as.factor(bac$type) ba.an<-aov(lm(day~type,date=bac)) summary(ba.an) boxplot(day~type,data=bac,col=”red”) 12: Calculatethefirst50powersof2,2*2,2*2*2,etc. Calculatethesquaresoftheintegernumbersfrom1to50. Whichpairsareequal,i.e.whichintegernumbersfulfillthecondition . Howmanypairsarethere? (UseRtosolveallthesequestions! ) >n=c(1: 50) >a=2^n >b=n^2 >x=a-b >n[x==0] [1]24 >sum(x==0) [1]2 >n[! ((x>0)|(x<0))] [1]24 >sum(! ((x>0)|(x<0))) [1]2 13: Calculatethesine,cosine,andthetangentfornumbersrangingfrom0to(withdistance0.1betweenthem). Rememberthattan(x)=sin(x)/cos(x).Nowcalculatethedifferencebetweentan(x)andsin(x)/cos(x)forthevaluesabove.Whichvaluesareexactlyequal? Whatisthemaximumdifference? Whatisthecauseofthedifferences? >A=seq(0,2*pi,0.1) >for(xinA) +if(sin(x)/cos(x)==tan(x)) +{print(x)} [1]0 [1]0.4 [1]0.5 [1]0.8 [1]1.4 [1]1.6 [1]1.7 [1]1.8 [1]1.9 [1]2 [1]2.1 [1]2.3 [1]2.4 [1]2.5 [1]2.7 [1]2.8 [1]2.9 [1]3 [1]3.1 [1]3.2 [1]3.3 [1]3.4 [1]3.6 [1]3.7 [1]3.8 [1]4 [1]4.1 [1]4.2 [1]4.5 [1]4.6 [1]4.8 [1]4.9 [1]5 [1]5.1 [1]5.2 [1]5.3 [1]5.4 [1]5.5 [1]5.7 [1]5.8 [1]6 [1]6.1 [1]6.2 >x=seq(from=0,to=2*pi,by=0.1) >s=sin(x) >c=cos(x) >t=tan(x) >d=s/c-t >x[md==abs(d)] [1]4.7 >x[d==0] [1]0.00.40.50.81.41.61.71.81.92.02.12.32.42.52.72.82.93.0 [19]3.13.23.33.43.63.73.84.04.14.24.54.64.84.95.05.15.25.3 [37]5.45.55.75.86.06.16.2 14: UsetheRhelproutines(notthemanuals)tofindouthowtousethefunctionsfloor,trunc,round,ceiling,andwhattheydo.Predictwhateachofthesefunctionswillgiveasananswerforthenumbers-3.7and+3.8.UseRtotestyourpredictions. ceiling向上取整 floor向下取整 trunc截尾取整 round按所保留的小数点位数四舍五入 signif按所需的有效数位数四舍五入 >ceiling(-3.7) [1]-3 >ceiling(-3.3) [1]-3 >ceiling(3.1) [1]4 >floor(-3.7) [1]-4 >floor(3.8) [1]3 >trunc(-3.7) [1]-3 >trunc(-3.3) [1]-3 >trunc(3.8) [1]3 >round(-3.7) [1]-4 >round(3.8) [1]4 >round(-3.74,digits=1) [1]-3.7 >round(-3.79,digits=1) [1]-3.8 >round(3.89,digits=1) [1]3.9 >round(3.84,digits=1) [1]3.8 >signif(-3.7) [1]-3.7 >signif(3.8) [1]3.8 >signif(3.8,digits=2) [1]3.8 >signif(-3.7,digits=1) [1]-4 >signif(-3.3,digits=1) [1]-3 >signif(3.1,digits=1) [1]3 >signif(3.8,digits=1) [1]4 15: 编写函数 定义函数: rcal<-function(x,y) { Z<-x^2+y^2; Result<-sqrt(z); Result; } 调用函数: Rcal(3,4) 16: 在原有图形上添加元素 X<-rnorm(100)#生成随机数 Hist(x,freq=F)#绘制直方图 Curve(dnom(x),add=T)#添加曲线 H<-hist(x,plot=F)#绘制直方图 Ylim<-range(0,h$density,dnorm(0))#设定纵轴的取值范围 Hist(x,freq=F,ylim=ylim)#绘制直方图 Curve(dnorm(x),add=T,col=”red”)#添加曲线 17: 生成0到2之间的50个随机数,分别命名为x,y X<-runif(50,0,2) Y<-runif(50,0,2) 绘图: 将主标题命名为“散点图”,横轴命名为“横坐标”,纵轴命名为“纵坐标” Plot(x,y,main=”散点图”,xlab=”横坐标”,ylab=”纵坐标”) Test(0.6,0.6,”textat(0.6,0.6)”) Abline(h=.6,v=.6) 18: 分步绘图: Plot(x,y,type=”n”,xlab=””,axes=F)#打开绘图窗口,不绘制任何对象 Point(x,y)#添加坐标点 Axis(at=seq(0.2,1.8,0.2),side=3)#添加纵轴 Box()#补齐散点图的边框 Title(main=”maintitle”,sub=”subtitle”,xlab=”x-lable”,”ylab=”y=lable”)#添加标题、副标题、横轴说明、纵轴说明 19: 一页多图(par()) Par(mfrow=c(2,2)) 20: 对一批涂料进行研究,确定搅拌速度对杂质含量的影响,数据如下,试进行回归分析 表: 搅拌速度对涂料中杂质的影响 转速 Rpm 20 22 24 26 28 30 32 34 36 38 40 42 杂质率 % 8.4 9.5 11.8 10.4 13.3 14.8 13.2 14.7 16.4 16.5 18.9 18.5 #将以下代码粘贴到编辑器中,另存为regression.r文件 Rate<-c(20,22,24,26,28,30,32,34,36,38,40,42) Impurity<-c(8.4,9.5,11.8,10.4,13.3,14.8,13.2,14.7,16.4,16.5,18.9,18.5) Plot(impurity~rate) Reg<-lm(impurity~rate) Abline(reg,col=”red”) Summary(reg) 三种运行方式 1通过source()函数运行 Source(“D: /regression.r”) 2通过R搅拌编辑器运行 路径: RGui>File>OpenScrip#Ctrl+R运行 3直接粘贴到R控制台 Ctrl+c,Ctrl+v
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 习题