1、一组学生参加了数学、科学与英语考试。为了给所有得学生确定一个单一得成绩衡量指标,需要将这些科目得成绩组合起来。另外,还想将前20%得学生评定为A,接下来20%得学生评定为B,以此类推。最后,希望按字母顺序对学生排序。 Excel中得数据表 StuId StuName Math Science English 1 John Davis 502 95 25 2 Angela Williams 465 67 12 3 Bull Jones 621 78 22 4 Cheryl Cushing 575 66 18 5 Reuven Ytzrhak
2、
454
96
15
6
Joel Knox
634
89
30
7
Mary Rayburn
576
78
37
8
Greg England
421
56
12
9
Brad Tmac
599
68
22
10
Tracy Mcgrady
666
100
38
1:输入数据——R语言导入xlsx
>install、packages("xlsx")
>library(xlsx)
>workbook<"D:/R /StuScore、xlsx"
>StuScore
3、
2:数据预处理——将变量进行标准化
> options(digits=2)#限定为2位小数
> afterscale
4、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(,"scaled:center") Math Science English 551 79 23 attr(,"scaled:scale") Math Science English 84、7 15、1 9、5 3:通过函数mean来计算各行得均值以及获得综合得分,并使用cbind将其添加到花名册中 > #
5、3在afterscale中计算标准差均值,并将其添加到StuScore
> score
6、 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
7、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
8、 0、60 0、21 0、18 0、50
5:使用逻辑运算符,把score转为等级(离散型)
> StuScore$grade[score>=afterquantile[1]]<"A"
> StuScore$grade[score
9、ile[4]]<"D"
> StuScore$grade[score
10、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
11、
9 9 Brad Tmac 599 68 22 0、10 E
10 10 Tracy Mcgrady 666 100 38 1、43 B
6:使用strsplit以空格为界把学生姓名拆分为姓氏与名字
> StuScore$StuName
12、[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:把
13、name分成Firstname与LastName,加入到StuScore中
> FirstName
14、 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
15、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 Br
16、ad 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 Cushin
17、g 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
18、6 Joel Knox Knox Joel Knox 634 89 30 0、78 B 10 Tracy Mcgrady Mcgrady Tracy Mcgrady 666 100 38 1、43 B 7 Mary Rayburn Rayburn Mary Rayburn 576 78 37 0、56 B 9 Brad Tmac Tmac Brad Tmac 599 68 22
19、 0、10 E
2 Angela Williams Williams Angela Williams 465 67 12 1、00 E
5 Reuven Ytzrhak Ytzrhak Reuven Ytzrhak 454 96 15 0、30 E
9:为StuScore绘制分组条形图
install、packages("vcd")
library(vcd)
fill_colors 20、1:length(StuScore$Science))
{
if(StuScore$Science[i]==100)
{
fill_colors 21、ll_colors,"blue")
}else{
fill_colors 22、ame)), #设定横坐标名称
border=NA, #条形框不设置边界线
font、main=4,
font、lab=3,
beside=TRUE)
legend(x=8、8,y=100, #左上角点得坐标
cex=、8, #缩放比例
inset=5,
c("Excellen 23、t","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 24、","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 25、验
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 26、type 27、tion 、
How many pairs are there?(Use R to solve all these questions!)
> n=c(1:50)
> a=2^n
> b=n^2
> x=ab
> n[x==0]
[1] 2 4
> sum(x==0)
[1] 2
> n[!((x>0)|(x<0))]
[1] 2 4
> sum(!((x>0)|(x<0)))
[1] 2
13: Calculate the sine, cosine, and the tangent for numbers ranging from 0 to (with dis 28、tance 0、1 between them)、
Remember that tan(x)=sin(x)/cos(x)、 Now calculate the difference between tan(x) and sin(x)/cos(x) for the values above、 Which values are exactly equal? What is the maximum difference? What is the cause of the differences?
> A=seq(0,2*pi,0、1)
> for(x in A)
+ if(sin(x)/cos 29、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
[ 30、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/ct
> x[md==abs(d)]
[1] 4、7
> x[d==0]
[1] 0、0 0、4 0、5 0、8 1、4 1、6 1、7 1、8 1、9 2、0 2、1 2、3 2、4 2、5 2、7 2、8 2、9 3、0
[19] 31、 3、1 3、2 3、3 3、4 3、6 3、7 3、8 4、0 4、1 4、2 4、5 4、6 4、8 4、9 5、0 5、1 5、2 5、3
[37] 5、4 5、5 5、7 5、8 6、0 6、1 6、2
14: Use the R help routines (not the manuals) to find out how to use the functions floor, trunc, round, ceiling, and what they do、 Predict what each of these functions will give as an answer f 32、or the numbers 3、7 and +3、8、 Use R to test your predictions、
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] 33、 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= 34、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 35、lot=F) #绘制直方图
Ylim 36、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=”main title”,sub=”subtitle”,xlab=”xlable”,”ylab =”y=lable”) #添加标题、副标题、横轴说明、纵轴说明
19:一页多图(par)
Par(mfrow=c(2,2))
20:对 37、一批涂料进行研究,确定搅拌速度对杂质含量得影响,数据如下,试进行回归分析
表:搅拌速度对涂料中杂质得影响
转速
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






