R语言实战上的题,做出来好费劲,,,会的太少了
一组学生参加了数学、科学和英语考试。为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来。另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推。最后,希望按字母顺序对学生排序。
1、数据录入
>StuId <- c(1:10)
>StuName <- c('John Davis','Angela Williams','Bull >Jones','Cheryl Cushing','Reuven Ytzrhak','Joel Knox','Mary Rayburn','Greg England','Brad Tmac','Tracy Mcgrady')
>Math <- c(502,465,621,575,454,634,576,421,599,666)
>Science <- c(95,67,78,66,96,89,78,56,68,10)
>English <- c(25,12,22,18,15,30,37,12,22,38)
>mydata <- data.frame(StuId,StuName,Math,Science,English)
>mydata
Stuld StuName Math Science English
1 1 John Davis 502 95 25
2 2 Angela Williams 465 67 12
3 3 Bull Jones 621 78 22
4 4 Cheryl Cushing 575 66 18
5 5 Reuven Ytzrhak 454 96 15
6 6 Joel Knox 634 89 30
7 7 Mary Rayburn 576 78 37
8 8 Greg England 421 56 12
9 9 Brad Tmac 599 68 22
10 10 Tracy Mcgrady 666 10 38
2、数据预处理-数据中心化、标准化
scale(data,center=T,scale=T)
center=T 数据中心化
scale=T 数据标准化
数据的中心化:指数据集中的各项数据减去数据集的均值
数据的标准化:值中心化后的数据在除以数据集的标准差
数据中心化和标准化的意义是一样的,为了消除量纲对数据结构的影响
>z <- scale(mydata[3:5])
>z
Math Science English
[1,] -0.5817963 0.98895866 0.2006619
[2,] -1.0184385 -0.13212808 -1.1722882
[3,] 0.8225395 0.30829885 -0.1161727
[4,] 0.2796870 -0.17216689 -0.5386189
[5,] -1.1482510 1.02899747 -0.8554535
[6,] 0.9759544 0.74872578 0.7287197
[7,] 0.2914882 0.30829885 1.4680005
[8,] -1.5376887 -0.57255501 -1.1722882
[9,] 0.5629144 -0.09208927 -0.1161727
[10,] 1.3535909 -2.41434036 1.5736120
attr(,"scaled:center")
Math Science English
551.3 70.3 23.1
attr(,"scaled:scale")
Math Science English
84.737569 24.975766 9.468662
3、计算每个学生成绩的均值
apply(X,MARGIN,FUN,…)
X:阵列包括矩阵
MARGIN:1表示矩阵行,2表示矩阵列,也可以是c(1,2)
FUN:表示具体的运算方式
>mydata$score <- apply(z,1,mean)
>mydata
Stuld StuName Math Science English score
1 1 John Davis 502 95 25 0.2026081
2 2 Angela Williams 465 67 12 -0.7742849
3 3 Bull Jones 621 78 22 0.3382219
4 4 Cheryl Cushing 575 66 18 -0.1436996
5 5 Reuven Ytzrhak 454 96 15 -0.3249024
6 6 Joel Knox 634 89 30 0.8177999
7 7 Mary Rayburn 576 78 37 0.6892625
8 8 Greg England 421 56 12 -1.0941773
9 9 Brad Tmac 599 68 22 0.1182175
10 10 Tracy Mcgrady 666 10 38 0.1709542
4、算出四分位数
quantile(x,probs) 求分位数。
x:待求分位数的数值型向量,
pobs:一个由[0,1]之间的概率值组成的数值向量
>q<-quantile(mydata$score,c(.8,.6,.4,.2))
>q
80% 60% 40% 20%
0.40843002 0.18361575 0.01345066 -0.41477887
5、根据四分位数给出分数等级
>mydata$level[score>=q[1]] <- "A"
>mydata$level[score>=q[2] & score<q[1]] <- "B"
>mydata$level[score>=q[3] & score<q[2]] <- "C"
>mydata$level[score>=q[4] & score<q[3]] <- "D"
>mydata$level[score<q[4]] <- "E"
6、拆分StuName列为FirName和LasName两列
拆分这里有两个方法,方法一是我用的笨方法;方法二使用sapply()函数
strsplit(x,split=”“)
x 是向量
split是以什么分隔
方法一
>name<-strsplit(mydata$StuName,split=" ")--这句语句错误 x应为向量
--Error in strsplit(mydata$StuName, split = " ") : non-character argument
>name<-strsplit(StuName,split=" ")--以空格分隔,
>name --把strsplit()应用到一个字符串组成的向量上会返回一个列表
[[1]]
[1] "John" "Davis"
[[2]]
[1] "Angela" "Williams"
[[3]]
[1] "Bull" "Jones"
[[4]]
[1] "Cheryl" "Cushing"
...
>name<-unlist(strsplit(StuName,split=" "))--将list形式转化成向量格式
> name
[1] "John" "Davis" "Angela" "Williams" "Bull" "Jones" "Cheryl" "Cushing" "Reuven"
[10] "Ytzrhak" "Joel" "Knox" "Mary" "Rayburn" "Greg" "England" "Brad" "Tmac"
[19] "Tracy" "Mcgrady"
>name<-matrix(name,ncol=2,byrow=T)
>name
[,1] [,2]
[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"
>mydata<-cbind(mydata,FirName,LasName)
>FirName<-name[,1]
>LasName<-name[,2]
方法二:
sapply() 提取列表中每个成分的第一个元素,
“[”是一个可以提取某个对象一部分的函数
>name<-strsplit((StuName)," ")
>lastname<-sapply(name,"[",2)
>lastname
[1] "Davis" "Williams" "Jones" "Cushing" "Ytzrhak" "Knox" "Rayburn" "England" "Tmac"
[10] "Mcgrady"
>firstname<-sapply(name,"[",1)
>firstname
[1] "John" "Angela" "Bull" "Cheryl" "Reuven" "Joel" "Mary" "Greg" "Brad" "Tracy"
7、合并&排序
>mydata<-cbind(mydata,FirName,LasName)
>mydata
Stuld Math Science English score FirName LasName level
4 4 575 66 18 -0.1436996 Cheryl Cushing D
1 1 502 95 25 0.2026081 John Davis B
8 8 421 56 12 -1.0941773 Greg England E
3 3 621 78 22 0.3382219 Bull Jones B
6 6 634 89 30 0.8177999 Joel Knox A
10 10 666 10 38 0.1709542 Tracy Mcgrady c
7 7 576 78 37 0.6892625 Mary Rayburn A
9 9 599 68 22 0.1182175 Brad Tmac c
2 2 465 67 12 -0.7742849 Angela Williams E
5 5 454 96 15 -0.3249024 Reuven Ytzrhak D
> options(digits=2)--保留小数点后两位
> mydata<-mydata[order(mydata$LasName,mydata$FirName),]--排序
> mydata
Stuld Math Science English score FirName LasName level
4 4 575 66 18 -0.14 Cheryl Cushing D
1 1 502 95 25 0.20 John Davis B
8 8 421 56 12 -1.09 Greg England E
3 3 621 78 22 0.34 Bull Jones B
6 6 634 89 30 0.82 Joel Knox A
10 10 666 10 38 0.17 Tracy Mcgrady c
7 7 576 78 37 0.69 Mary Rayburn A
9 9 599 68 22 0.12 Brad Tmac c
2 2 465 67 12 -0.77 Angela Williams E
5 5 454 96 15 -0.32 Reuven Ytzrhak D