5.3 数据处理难题的一套解决方案

5.1节中提出的问题是:将学生的各科考试成绩组合为单一的成绩衡量指标、基于相对名次(前20%,下20%,等等)给出从A到F的评分、根据学生姓氏和名字的首字母对花名册进行排序。代码清单5-6给出了一种解决方案。

代码清单5-6 示例的一种解决方案

  1. > options(digits=2)
  2. > Student <- c("John Davis", "Angela Williams", "Bullwinkle Moose",
  3. "David Jones", "Janice Markhammer", "Cheryl Cushing",
  4. "Reuven Ytzrhak", "Greg Knox", "Joel England",
  5. "Mary Rayburn")
  6. > Math <- c(502, 600, 412, 358, 495, 512, 410, 625, 573, 522)
  7. > Science <- c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86)
  8. > English <- c(25, 22, 18, 15, 20, 28, 15, 30, 27, 18)
  9. > roster <- data.frame(Student, Math, Science, English,
  10. stringsAsFactors=FALSE)
  11. > z <- scale(roster[,2:4]) # 计算综合得分
  12. > score <- apply(z, 1, mean)
  13. > roster <- cbind(roster, score)
  14. > y <- quantile(score, c(.8,.6,.4,.2)) # 对学生评分
  15. > roster$grade[score >= y[1]] <- "A"
  16. > roster$grade[score < y[1] & score >= y[2]] <- "B"
  17. > roster$grade[score < y[2] & score >= y[3]] <- "C"
  18. > roster$grade[score < y[3] & score >= y[4]] <- "D"
  19. > roster$grade[score < y[4]] <- "F"
  20. > name <- strsplit((roster$Student), " ") # 抽取姓氏和名字
  21. > lastname <- sapply(name, "[", 2)
  22. > firstname <- sapply(name, "[", 1)
  23. > roster <- cbind(firstname,lastname, roster[,-1])
  24. > roster <- roster[order(lastname,firstname),] # 根据姓氏和名字排序
  25. > roster
  26. Firstname Lastname Math Science English score grade
  27. 6 Cheryl Cushing 512 85 28 0.35 C
  28. 1 John Davis 502 95 25 0.56 B
  29. 9 Joel England 573 89 27 0.70 B
  30. 4 David Jones 358 82 15 -1.16 F
  31. 8 Greg Knox 625 95 30 1.34 A
  32. 5 Janice Markhammer 495 75 20 -0.63 D
  33. 3 Bullwinkle Moose 412 80 18 -0.86 D
  34. 10 Mary Rayburn 522 86 18 -0.18 C
  35. 2 Angela Williams 600 99 22 0.92 A
  36. 7 Reuven Ytzrhak 410 80 15 -1.05 F

以上代码写得比较紧凑,逐步分解如下。

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

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

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

  1. > z <- scale(roster[,2:4])
  2. > z
  3. Math Science English
  4. [1,] 0.013 1.078 0.587
  5. [2,] 1.143 1.591 0.037
  6. [3,] -1.026 -0.847 -0.697
  7. [4,] -1.649 -0.590 -1.247
  8. [5,] -0.068 -1.489 -0.330
  9. [6,] 0.128 -0.205 1.137
  10. [7,] -1.049 -0.847 -1.247
  11. [8,] 1.432 1.078 1.504
  12. [9,] 0.832 0.308 0.954
  13. [10,] 0.243 -0.077 -0.697

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

  1. > score <- apply(z, 1, mean)
  2. > roster <- cbind(roster, score)
  3. > roster
  4. Student Math Science English score
  5. 1 John Davis 502 95 25 0.559
  6. 2 Angela Williams 600 99 22 0.924
  7. 3 Bullwinkle Moose 412 80 18 -0.857
  8. 4 David Jones 358 82 15 -1.162
  9. 5 Janice Markhammer 495 75 20 -0.629
  10. 6 Cheryl Cushing 512 85 28 0.353
  11. 7 Reuven Ytzrhak 410 80 15 -1.048
  12. 8 Greg Knox 625 95 30 1.338
  13. 9 Joel England 573 89 27 0.698
  14. 10 Mary Rayburn 522 86 18 -0.177

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

  1. > y <- quantile(roster$score, c(.8,.6,.4,.2))
  2. > y
  3. 80% 60% 40% 20%
  4. 0.74 0.44 -0.36 -0.89

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

  1. > roster$grade[score >= y[1]] <- "A"
  2. > roster$grade[score < y[1] & score >= y[2]] <- "B"
  3. > roster$grade[score < y[2] & score >= y[3]] <- "C"
  4. > roster$grade[score < y[3] & score >= y[4]] <- "D"
  5. > roster$grade[score < y[4]] <- "F"
  6. > roster
  7. Student Math Science English score grade
  8. 1 John Davis 502 95 25 0.559 B
  9. 2 Angela Williams 600 99 22 0.924 A
  10. 3 Bullwinkle Moose 412 80 18 -0.857 D
  11. 4 David Jones 358 82 15 -1.162 F
  12. 5 Janice Markhammer 495 75 20 -0.629 D
  13. 6 Cheryl Cushing 512 85 28 0.353 C
  14. 7 Reuven Ytzrhak 410 80 15 -1.048 F
  15. 8 Greg Knox 625 95 30 1.338 A
  16. 9 Joel England 573 89 27 0.698 B
  17. 10 Mary Rayburn 522 86 18 -0.177 C

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

  1. > name <- strsplit((roster$Student), " ")
  2. > name
  3. [[1]]
  4. [1] "John" "Davis"
  5. [[2]]
  6. [1] "Angela" "Williams"
  7. [[3]]
  8. [1] "Bullwinkle" "Moose"
  9. [[4]]
  10. [1] "David" "Jones"
  11. [[5]]
  12. [1] "Janice" "Markhammer"
  13. [[6]]
  14. [1] "Cheryl" "Cushing"
  15. [[7]]
  16. [1] "Reuven" "Ytzrhak"
  17. [[8]]
  18. [1] "Greg" "Knox"
  19. [[9]]
  20. [1] "Joel" "England"
  21. [[10]]
  22. [1] "Mary" "Rayburn"

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

  1. > Firstname <- sapply(name, "[", 1)
  2. > Lastname <- sapply(name, "[", 2)
  3. > roster <- cbind(Firstname, Lastname, roster[,-1])
  4. > roster
  5. Firstname Lastname Math Science English score grade
  6. 1 John Davis 502 95 25 0.559 B
  7. 2 Angela Williams 600 99 22 0.924 A
  8. 3 Bullwinkle Moose 412 80 18 -0.857 D
  9. 4 David Jones 358 82 15 -1.162 F
  10. 5 Janice Markhammer 495 75 20 -0.629 D
  11. 6 Cheryl Cushing 512 85 28 0.353 C
  12. 7 Reuven Ytzrhak 410 80 15 -1.048 F
  13. 8 Greg Knox 625 95 30 1.338 A
  14. 9 Joel England 573 89 27 0.698 B
  15. 10 Mary Rayburn 522 86 18 -0.177 C

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

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

瞧!小事一桩!

完成这些任务的方式有许多,只是以上代码体现了相应函数的设计初衷。现在到了学习控制结构和自己编写函数的时候了。