步骤1

原始的学生花名册已经给出了。options(digits=2)限定了输出小数点后数字的位数,并且让输出更容易阅读。

options(digits=2)
Student <- c("John Davis", "Angela Williams", 
             "Bullwinkle Moose", "David Jones", 
             "Janice Markhammer", "Cheryl Cushing",
             "Reuven Ytzrhak", "Greg Knox", "Joel England",
             "Mary Rayburn")
Math <- c(502, 600, 412, 358, 495, 512, 410, 625, 573, 522)
Science <- c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86)
English <- c(25, 22, 18, 15, 20, 28, 15, 30, 27, 18)
roster <- data.frame(Student, Math, Science, English,
                     stringsAsFactors=FALSE)
roster
##              Student Math Science English
## 1         John Davis  502      95      25
## 2    Angela Williams  600      99      22
## 3   Bullwinkle Moose  412      80      18
## 4        David Jones  358      82      15
## 5  Janice Markhammer  495      75      20
## 6     Cheryl Cushing  512      85      28
## 7     Reuven Ytzrhak  410      80      15
## 8          Greg Knox  625      95      30
## 9       Joel England  573      89      27
## 10      Mary Rayburn  522      86      18
步骤2

由于数学、科学和英语考试的分值不同(均值和标准差都相去甚远),在组合之前需要先让它们变得可以比较。一种方法是将变量进行标准化,这样每科考试的成绩就都是单位标准差来表示,而不是以原始的尺度来表示了。这个过程可以使用scale()函数来实现:

z <- scale(roster[,2:4])
z
##         Math Science English
##  [1,]  0.013   1.078   0.587
##  [2,]  1.143   1.591   0.037
##  [3,] -1.026  -0.847  -0.697
##  [4,] -1.649  -0.590  -1.247
##  [5,] -0.068  -1.489  -0.330
##  [6,]  0.128  -0.205   1.137
##  [7,] -1.049  -0.847  -1.247
##  [8,]  1.432   1.078   1.504
##  [9,]  0.832   0.308   0.954
## [10,]  0.243  -0.077  -0.697
## attr(,"scaled:center")
##    Math Science English 
##     501      87      22 
## attr(,"scaled:scale")
##    Math Science English 
##    86.7     7.8     5.5
步骤3

然后,可以通过mean()来计算各行的均值以获得综合得分,并使用函数cbind()将其添加到花名册中:

score <- apply(z,1,mean)
roster <- cbind(roster,score)
roster
##              Student Math Science English score
## 1         John Davis  502      95      25  0.56
## 2    Angela Williams  600      99      22  0.92
## 3   Bullwinkle Moose  412      80      18 -0.86
## 4        David Jones  358      82      15 -1.16
## 5  Janice Markhammer  495      75      20 -0.63
## 6     Cheryl Cushing  512      85      28  0.35
## 7     Reuven Ytzrhak  410      80      15 -1.05
## 8          Greg Knox  625      95      30  1.34
## 9       Joel England  573      89      27  0.70
## 10      Mary Rayburn  522      86      18 -0.18
步骤4

函数quantile()给出了学生综合得分的百分位数。可以看到,成绩为A的分界点为0.74,B的分界点为0.44,等等。

y <- quantile(roster$score,c(0.8,0.6,0.4,0.2))
y
##   80%   60%   40%   20% 
##  0.74  0.44 -0.36 -0.89
步骤5

通过使用逻辑运算符,你可以将学生的百分位数排名重编码为一个新的类别型成绩变量。下面在数据框roster中创建了变量grade。

roster$grade[roster$score >= y[1]] <- "A"
roster$grade[roster$score < y[1] & roster$score >= y[2]] <- "B"
roster$grade[roster$score < y[2] & roster$score >= y[3]] <- "C"
roster$grade[roster$score < y[3] & roster$score >= y[4]] <- "D"
roster$grade[roster$score < y[4]] <- "F"
roster
##              Student Math Science English score grade
## 1         John Davis  502      95      25  0.56     B
## 2    Angela Williams  600      99      22  0.92     A
## 3   Bullwinkle Moose  412      80      18 -0.86     D
## 4        David Jones  358      82      15 -1.16     F
## 5  Janice Markhammer  495      75      20 -0.63     D
## 6     Cheryl Cushing  512      85      28  0.35     C
## 7     Reuven Ytzrhak  410      80      15 -1.05     F
## 8          Greg Knox  625      95      30  1.34     A
## 9       Joel England  573      89      27  0.70     B
## 10      Mary Rayburn  522      86      18 -0.18     C
步骤6

你将使用函数strsplit()以空格为界把学生姓名拆分为姓氏和名字。把strsplit()应用到一个字符串组成的向量上返回一个列表:

name <- strsplit(roster$Student," ")
name
## [[1]]
## [1] "John"  "Davis"
## 
## [[2]]
## [1] "Angela"   "Williams"
## 
## [[3]]
## [1] "Bullwinkle" "Moose"     
## 
## [[4]]
## [1] "David" "Jones"
## 
## [[5]]
## [1] "Janice"     "Markhammer"
## 
## [[6]]
## [1] "Cheryl"  "Cushing"
## 
## [[7]]
## [1] "Reuven"  "Ytzrhak"
## 
## [[8]]
## [1] "Greg" "Knox"
## 
## [[9]]
## [1] "Joel"    "England"
## 
## [[10]]
## [1] "Mary"    "Rayburn"
步骤7

你可以使用函数sapply()提取列表中每个成分的第一个元素,放入一个储存名字的向量,并提取每个成分的第二个元素,放入一个储存姓氏的向量。“[”是一个可以提取某个对象的一部分的函数——在这里它是用来提取列表name各成分中的第一个或第二个元素的。你将使用cbind()将它们添加到花名册中。由于已经不再需要student变量,可以将其丢弃(在下标中使用-1)。

Firstname <- sapply(name,"[",1)
Lastname <- sapply(name,"[",2)
roster <- cbind(Firstname, Lastname, roster[-1])
roster
##     Firstname   Lastname Math Science English score grade
## 1        John      Davis  502      95      25  0.56     B
## 2      Angela   Williams  600      99      22  0.92     A
## 3  Bullwinkle      Moose  412      80      18 -0.86     D
## 4       David      Jones  358      82      15 -1.16     F
## 5      Janice Markhammer  495      75      20 -0.63     D
## 6      Cheryl    Cushing  512      85      28  0.35     C
## 7      Reuven    Ytzrhak  410      80      15 -1.05     F
## 8        Greg       Knox  625      95      30  1.34     A
## 9        Joel    England  573      89      27  0.70     B
## 10       Mary    Rayburn  522      86      18 -0.18     C
步骤8

最后,可以使用函数order()依姓氏和名字对数据集进行排序:

roster[order(Lastname,Firstname),]
##     Firstname   Lastname Math Science English score grade
## 6      Cheryl    Cushing  512      85      28  0.35     C
## 1        John      Davis  502      95      25  0.56     B
## 9        Joel    England  573      89      27  0.70     B
## 4       David      Jones  358      82      15 -1.16     F
## 8        Greg       Knox  625      95      30  1.34     A
## 5      Janice Markhammer  495      75      20 -0.63     D
## 3  Bullwinkle      Moose  412      80      18 -0.86     D
## 10       Mary    Rayburn  522      86      18 -0.18     C
## 2      Angela   Williams  600      99      22  0.92     A
## 7      Reuven    Ytzrhak  410      80      15 -1.05     F