(1)按分数给等级
(2)按姓氏排序
options(digits=2)#限定输出小数点后数字的位数
Students<-c("John Davis","Angela Williams","Bullwinkle Moose","David Jones","Janice Markhammer","Cheryl Cushing","Reuven Ytzrhak","Greg Knox","Joel England","Mary Rayburn")Students
str(Students)
Math<-c(502,600,412,358,495,512,410,625,573,522)
Math
Science<-c(95,99,80,82,75,85,80,95,89,86)
Science
English<-c(25,22,18,15,20,28,15,30,27,18)
English
roster<-data.frame(Students,Math,Science,English,stringsAsFactors=FALSE)
z<-scale(roster[,2:4])#标准化
z
score<-apply(z,1,mean)?#计算综合得分
score
roster<-cbind(roster,score)
roster
?quantile
y<-quantile(score,c(0.8,0.6,0.4,0.2))
y
roster$grade[score>=y[1]]<-"A"
roster$grade[score<y[1]&score>=y[2]]<-"B"
roster$grade[score<y[2]&score>=y[3]]<-"C"
roster$grade[score<y[3]&score>=y[4]]<-"D"
roster$grade[score<y[4]]<-"E"
roster
name<-strsplit((roster$Students)," ")
lastname<-sapply(name,"[",2)
firstname<-sapply(name,"[",1)
roster<-roster[order(firstname,lastname),]
roster