R语言习题

2019-02-15 16:10

一组学生参加了数学、科学和英语考试。为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来。另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推。最后,希望按字母顺序对学生排序。 Excel中的数据表 StuId StuName Math Science English

1 John Davis 2 Angela Williams 3 Bull Jones 4 Cheryl Cushing 5 Reuven Ytzrhak 6 Joel Knox 7 Mary Rayburn 8 Greg England 9 Brad Tmac 10 Tracy Mcgrady

1:输入数据——R语言导入xlsx >install.packages(\>library(xlsx)

>workbook<-\>StuScore<-read.xlsx(workbook,1) >StuScore

2:数据预处理——将变量进行标准化 > options(digits=2)#限定为2位小数 > afterscale<-scale(StuScore[,3:5]) > afterscale

Math Science English

[1,] -0.58 1.040 0.20 [2,] -1.02 -0.815 -1.17 [3,] 0.82 -0.086 -0.12 [4,] 0.28 -0.881 -0.54 [5,] -1.15 1.106 -0.86 [6,] 0.98 0.643 0.73 [7,] 0.29 -0.086 1.47 [8,] -1.54 -1.544 -1.17 [9,] 0.56 -0.749 -0.12 [10,] 1.35 1.372 1.57 attr(,\

Math Science English 551 79 23 attr(,\

Math Science English 84.7 15.1 9.5

502 95 465 67 621 78 575 66 454 96 634 89 576 78 421 56 599 68 666 100 25 12 22 18 15 30 37 12 22 38

3:通过函数mean()来计算各行的均值以及获得综合得分,并使用cbind()将其添加到花名册中 > #3在afterscale中计算标准差均值,并将其添加到StuScore > score<-apply(afterscale,1,mean)#1表示行,mean表示均值函数 > StuScore<-cbind(StuScore,score) > StuScore

StuId StuName Math Science English score

1 1 John Davis 502 95 25 0.22 2 2 Angela Williams 465 67 12 -1.00 3 3 Bull Jones 621 78 22 0.21 4 4 Cheryl Cushing 575 66 18 -0.38 5 5 Reuven Ytzrhak 454 96 15 -0.30 6 6 Joel Knox 634 89 30 0.78 7 7 Mary Rayburn 576 78 37 0.56 8 8 Greg England 421 56 12 -1.42 9 9 Brad Tmac 599 68 22 -0.10 10 10 Tracy Mcgrady 666 100 38 1.43

4:函数quantile()给出学生综合得分的百分位数

quantile(x,probs):求分位数,其中x为待求分位数的数值型向量,probs为一个由[0,1]之间的概率值组成的数值向量

> afterquantile<-quantile(score,c(.8,.6,.4,.2)) > afterquantile

80% 60% 40% 20% 0.60 0.21 -0.18 -0.50

5:使用逻辑运算符,把score转为等级(离散型) > StuScore$grade[score>=afterquantile[1]]<-\

> StuScore$grade[score=afterquantile[2]]<-\> StuScore$grade[score=afterquantile[3]]<-\> StuScore$grade[score=afterquantile[4]]<-\> StuScore$grade[score StuScore

StuId StuName Math Science English score grade 1 1 John Davis 502 95 25 0.22 B 2 2 Angela Williams 465 67 12 -1.00 E 3 3 Bull Jones 621 78 22 0.21 B 4 4 Cheryl Cushing 575 66 18 -0.38 E 5 5 Reuven Ytzrhak 454 96 15 -0.30 E 6 6 Joel Knox 634 89 30 0.78 B 7 7 Mary Rayburn 576 78 37 0.56 B 8 8 Greg England 421 56 12 -1.42 E 9 9 Brad Tmac 599 68 22 -0.10 E 10 10 Tracy Mcgrady 666 100 38 1.43 B

6:使用strsplit()以空格为界把学生姓名拆分为姓氏和名字 > StuScore$StuName<-as.character(StuScore$StuName) > is.character(StuScore$StuName) [1] TRUE

> name<-strsplit(StuScore$StuName,\> name [[1]]

[1] \ \[[2]]

[1] \ \[[3]]

[1] \ \[[4]]

[1] \ \[[5]]

[1] \ \[[6]]

[1] \[[7]]

[1] \ \[[8]]

[1] \ \[[9]]

[1] \[[10]]

[1] \ \

7:把name分成Firstname和LastName,加入到StuScore中 > FirstName<-sapply(name,\> LastName<-sapply(name,\

> StuScore<-cbind(FirstName,LastName,StuScore[,-1]) > StuScore

FirstName LastName LastName StuName Math Science English score grade 1 John Davis Davis John Davis 502 95 25 0.22 B 2 Angela Williams Williams Angela Williams 465 67 12 -1.00 E 3 Bull Jones Jones Bull Jones 621 78 22 0.21 B 4 Cheryl Cushing Cushing Cheryl Cushing 575 66 18 -0.38 E 5 Reuven Ytzrhak Ytzrhak Reuven Ytzrhak 454 96 15 -0.30 E 6 Joel Knox Knox Joel Knox 634 89 30 0.78 B 7 Mary Rayburn Rayburn Mary Rayburn 576 78 37 0.56 B 8 Greg England England Greg England 421 56 12 -1.42 E 9 Brad Tmac Tmac Brad Tmac 599 68 22 -0.10 E 10 Tracy Mcgrady Mcgrady Tracy Mcgrady 666 100 38 1.43 B

8:order排序

> StuScore[order(LastName,FirstName),]

FirstName LastName LastName StuName Math Science English score grade

4 Cheryl Cushing Cushing Cheryl Cushing 575 66 18 -0.38 E 1 John Davis Davis John Davis 502 95 25 0.22 B 8 Greg England England Greg England 421 56 12 -1.42 E 3 Bull Jones Jones Bull Jones 621 78 22 0.21 B 6 Joel Knox Knox Joel Knox 634 89 30 0.78 B 10 Tracy Mcgrady Mcgrady Tracy Mcgrady 666 100 38 1.43 7 Mary Rayburn Rayburn Mary Rayburn 576 78 37 0.56 9 Brad Tmac TmacBrad Tmac 599 68 22 -0.10 E 2 Angela WilliamsWilliamsAngela Williams 465 67 12 -1.00 E

5 Reuven Ytzrhak Ytzrhak Reuven Ytzrhak 454 96 15 -0.30

9:为StuScore绘制分组条形图 install.packages(\library(vcd)

fill_colors<-c() #不同的等级,不同的颜色显示 for(i in 1:length(StuScore$Science)) {

if(StuScore$Science[i]==100) {

fill_colors<-c(fill_colors,\}

else if(StuScore$Science[i]<100&&StuScore$Science[i]>=80) {

fill_colors<-c(fill_colors,\ }

else if(StuScore$Science[i]<80&&StuScore$Science[i]>=60) {

fill_colors<-c(fill_colors,\ }else{

fill_colors<-c(fill_colors,\ } }

barplot(StuScore$Science, #条形图

main=\

xlab=\

col=fill_colors,

B B E names.arg=(paste(substr(FirstName,1,1),\ #设定横坐标名称 border=NA, #条形框不设置边界线 font.main=4, font.lab=3, beside=TRUE)

legend(x=8.8,y=100, #左上角点的坐标 cex=.8, #缩放比例 inset=5,

c(\

pch=c(15,16,17,19), #图例中的符号 col=c(\

bg=\ #背景色

xpd=TRUE, #可以在绘图区之外显示 text.font=8, text.width=.8,

text.col=c(\

10:现有6名患者的身高和体重,检验体重除以身高的平方是否等于22.5. 1 2 3 4 5 编号 身高m 体重kg 1.75 60 1.80 72 1.65 57 1.90 90 1.74 95 6 1.91 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:Calculate the first 50 powers of 2, 2*2, 2*2*2, etc.

Calculate the squares of the integer numbers from 1 to 50.

n2Which pairs are equal, i.e. which integer numbers fulfill the condition 2?n.


R语言习题.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:市委宣传部信息科上半年工作总结及下半年计划

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: