资源描述
一组学生参加了数学、科学与英语考试。为了给所有得学生确定一个单一得成绩衡量指标,需要将这些科目得成绩组合起来。另外,还想将前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
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<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(,"scaled:center")
Math Science English
551 79 23
attr(,"scaled:scale")
Math Science English
84、7 15、1 9、5
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]]<"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"
> 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] "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
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 B
7 Mary Rayburn Rayburn Mary Rayburn 576 78 37 0、56 B
9 Brad Tmac Tmac Brad Tmac 599 68 22 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<c #不同得等级,不同得颜色显示
for(i in 1:length(StuScore$Science))
{
if(StuScore$Science[i]==100)
{
fill_colors<c(fill_colors,"red")
}
else if(StuScore$Science[i]<100&&StuScore$Science[i]>=80)
{
fill_colors<c(fill_colors,"yellow")
}
else if(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="Science Score",
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: Calculate the first 50 powers of 2, 2*2, 2*2*2, etc、
Calculate the squares of the integer numbers from 1 to 50、
Which pairs are equal, i、e、 which integer numbers fulfill the condition 、
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 distance 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(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/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] 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 for 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] 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,”text at(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=”main title”,sub=”subtitle”,xlab=”xlable”,”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> Scrip #Ctrl+R运行
3 直接粘贴到R控制台
Ctrl+c,Ctrl+v
展开阅读全文