word文档R语言习题

内容

一组学生参加了数学、科学和英语考试。为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来。另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推。最后,希望按字母顺序对学生排序。Excel中的数据表StuIdStuNameMathScienceEnglish1JohnDavis50295252AngelaWilliams46567123BullJones62178224CherylCushing57566185ReuvenYtzrhak45496156JoelKnox63489307MaryRayburn57678378GregEngland42156129BradTmac599682210TracyMcgrady666100381:输入数据——R语言导入xlsx>install.packages("xlsx")>library(xlsx)>workbook<-"D:/R/StuScore.xlsx">StuScore<-read.xlsx(workbook,1)>StuScore2:数据预处理——将变量进行标准化>options(digits=2)#限定为2位小数>afterscale<-scale(StuScore[,3:5])>afterscaleMathScienceEnglish[1,]-0.581.0400.20[2,]-1.02-0.815-1.17[3,]0.82-0.086-0.12[4,]0.28-0.881-0.54[5,]-1.151.106-0.86[6,]0.980.6430.73[7,]0.29-0.0861.47[8,]-1.54-1.544-1.17[9,]0.56-0.749-0.12[10,]1.351.3721.57attr(,"scaled:center")MathScienceEnglish5517923attr(,"scaled:scale")MathScienceEnglish84.715.19.5在线考试答题系统_无需自己建站_3步点击试用广告在线考试答题系统支持屏幕分享/课件演示/音视频同步,多终端自由切换,APP/微信/PC端。查看详情>3:通过函数mean()来计算各行的均值以及获得综合得分,并使用cbind()将其添加到花名册中>#3在afterscale中计算标准差均值,并将其添加到StuScore>score<-apply(afterscale,1,mean)#1表示行,mean表示均值函数>StuScore<-cbind(StuScore,score)>StuScoreStuIdStuNameMathScienceEnglishscore11JohnDavis50295250.2222AngelaWilliams4656712-1.0033BullJones62178220.2144CherylCushing5756618-0.3855ReuvenYtzrhak4549615-0.3066JoelKnox63489300.7877MaryRayburn57678370.5688GregEngland4215612-1.4299BradTmac5996822-0.101010TracyMcgrady666100381.434:函数quantile()给出学生综合得分的百分位数quantile(x,probs):求分位数,其中x为待求分位数的数值型向量,probs为一个由[0,1]之间的概率值组成的数值向量>afterquantile<-quantile(score,c(.8,.6,.4,.2))>afterquantile80%60%40%20%0.600.21-0.18-0.505:使用逻辑运算符,把score转为等级(离散型)>StuScore$grade[score>=afterquantile[1]]<-"A">StuScore$grade[score<afterquantile[1]&&score>=afterquantile[2]]<-"B">StuScore$grade[score<afterquantile[2]&&score>=afterquantile[3]]<-"C">StuScore$grade[score<afterquantile[3]&&score>=afterquantile[4]]<-"D">StuScore$grade[score<afterquantile[4]]<-"E">StuScoreStuIdStuNameMathScienceEnglishscoregrade11JohnDavis50295250.22B22AngelaWilliams4656712-1.00E33BullJones62178220.21B44CherylCushing5756618-0.38E55ReuvenYtzrhak4549615-0.30E66JoelKnox63489300.78B77MaryRayburn57678370.56B88GregEngland4215612-1.42E99BradTmac5996822-0.10E1010TracyMcgrady666100381.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])>StuScoreFirstNameLastNameLastNameStuNameMathScienceEnglishscoregrade1JohnDavisDavisJohnDavis50295250.22B2AngelaWilliamsWilliamsAngelaWilliams4656712-1.00E3BullJonesJonesBullJones62178220.21B4CherylCushingCushingCherylCushing5756618-0.38E5ReuvenYtzrhakYtzrhakReuvenYtzrhak4549615-0.30E6JoelKnoxKnoxJoelKnox63489300.78B7MaryRayburnRayburnMaryRayburn57678370.56B8GregEnglandEnglandGregEngland4215612-1.42E9BradTmacTmacBradTmac5996822-0.10E10TracyMcgradyMcgradyTracyMcgrady666100381.43B有没有什么答题软件?微信扫码考试,点击注册搭建考试广告「问卷星」免费在线考试搭建平台,选择题\填空题\简答题等49种题型自主搭配,查看详情>8:order排序>StuScore[order(LastName,FirstName),]FirstNameLastNameLastNameStuNameMathScienceEnglishscoregrade4CherylCushingCushingCherylCushing5756618-0.38E1JohnDavisDavisJohnDavis50295250.22B8GregEnglandEnglandGregEngland4215612-1.42E3BullJonesJonesBullJones62178220.21B6JoelKnoxKnoxJoelKnox63489300.78B10TracyMcgradyMcgradyTracyMcgrady666100381.43B7MaryRayburnRayburnMaryRayburn57678370.56B9BradTmacTmacBradTmac5996822-0.10E2AngelaWilliamsWilliamsAngelaWilliams4656712-1.00E5ReuvenYtzrhakYtzrhakReuvenYtzrhak4549615-0.30E9:为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,快考题——答题软件免费创建考试平台广告答题软件,提供在线考试,线上自测练习和模拟考试,移动PC同步考试,免安装查看详情>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.编号123456身高m1.751.801.651.901.741.91体重kg607257909572height<-c(1.75,1.80,1.65,1.90,1.74,1.91)weight<-c(60,72,57,90,95,72)sq.height<-height^2ratio<-weight/sq.heightt.test(ratio,mu=22.5)#t检验11:将三种不同菌型的伤寒病毒a,b,c分别接种于100,9,11只小白鼠上,观察其存活天数,问三种菌型下小白鼠的平均存活天数是否有显著差异。a菌株:2,4,3,2,4,7,7,2,5,4b菌株:5,6,8,5,10,7,12,6,6c菌株: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.whichintegernumbersfulfillthecondition22nn.

浏览:39
下载量:-
下载币:5
日期:2020-11-20
12
收藏
         投诉 / 报错
  • /12
1
2
3
4
5
6
7
8
9
10
11
12