参考这篇博客练习R的基本操作
一、导入数据
1 #1数据输入
2 install.packages(“xlsx”)
3 library(xlsx)
4 workbook<-“F:/Machine Learning/R Basic/Rtest1.xlsx”
5 StuScore<-read.xlsx(workbook,1)#1表示sheet1
6 StuScore
二、数据预处理,将数据标准化
2 > options(digits=2)#限定为2位小数
3 > afterscale<-scale(StuScore[,3:5])
4 > afterscale
5 Math Science English
6 [1,] -0.58 1.040 0.20
7 [2,] -1.02 -0.815 -1.17
8 [3,] 0.82 -0.086 -0.12
9 [4,] 0.28 -0.881 -0.54
10 [5,] -1.15 1.106 -0.86
11 [6,] 0.98 0.643 0.73
12 [7,] 0.29 -0.086 1.47
13 [8,] -1.54 -1.544 -1.17
14 [9,] 0.56 -0.749 -0.12
15 [10,] 1.35 1.372 1.57
16 attr(,”scaled:center”)
17 Math Science English
18 551 79 23
19 attr(,”scaled:scale”)
20 Math Science English
21 84.7 15.1 9.5
三、函数quantile()给出学生综合得分的百分位数
afterquantile<-quantile(score,c(.8,.6,.4,.2))
afterquantile
80% 60% 40% 20%
0.43 0.15 -0.13 -0.51
四、将分数转成等级
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
五、使用strsplit()以空格为界把学生姓名拆分为姓氏和名字
StuScore
StuName<−as.character(StuScore
StuName)
name<-strsplit(StuScore$StuName,” “)
六、把name分成Firstname和LastName,加入到StuScore中
#7把name分成Firstname和LastName,加入到StuScore中
> FirstName<-sapply(name,"[",1)
> LastName<-sapply(name,"[",2)
> StuScore<-cbind(FirstName,LastName,StuScore[,-1])
七、order排序
StuScore[order(LastName,FirstName),]
八、绘制成条形图
代码与博客中的一样,为了显示效果,对参数稍作调整