【发布时间】:2018-08-17 07:19:03
【问题描述】:
我有以下df
dat <- data.frame(Cases = c("Student3","Student3","Student3","Student1","Student1",
"Student2","Student2","Student2","Student4"), Class = rep("Math", 9),
Scores = c(9,5,2,7,3,8,5,1,7), stringsAsFactors = F)
> dat
Cases Class Scores
1 Student3 Math 9
2 Student3 Math 5
3 Student3 Math 2
4 Student1 Math 7
5 Student1 Math 3
6 Student2 Math 8
7 Student2 Math 5
8 Student2 Math 1
9 Student4 Math 7
另一方面,我有另一个包含以下信息的 df:
d <- data.frame(Cases = c("Student3", "Student1",
"Student2", "Student4"), Class = rep("Math", 4), stringsAsFactors = F)
Cases Class
1 Student3 Math
2 Student1 Math
3 Student2 Math
4 Student4 Math
有了这两个,我想为每个student提取最高的scores。所以我的输出看起来像这样:
> dat_output
Cases Class Scores
1 Student3 Math 9
2 Student1 Math 7
3 Student2 Math 8
4 Student4 Math 7
我尝试使用merge,但它并没有只提取最高的scores。
【问题讨论】:
-
@Tjebo 是的,还有很多其他帖子都带有“find max per group”,但这篇帖子有“过滤”步骤:只对同样在 d 数据框中的学生子集执行此操作。跨度>
-
@Tjebo 请仔细阅读问题,它不要求组中的最大值。
-
@RonakShah 这个标题跟我说的是另一种语言。