如何按组获取最大值 [英] How to get the maximum value by group

查看:48
本文介绍了如何按组获取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame,其中有两列:yearscore.年份从2000年到2012年,每年可以多次列出.在分数"列中,我列出了每年的所有分数,每一行都有不同的分数.

I have a data.frame with two columns: year and score. The years go from 2000-2012 and each year can be listed multiple times. In the score column I list all the scores for each year with each row having a different score.

我想做的是对data.frame进行过滤,因此仅保留每年得分最高的行.

What I'd like to do is filter the data.frame so only the rows with the maximum scores for each year remain.

作为一个小例子,如果我有

So as a tiny example if I have

year score
2000    18
2001    22
2000    21

我只想返回

year score
2001    22
2000    21

推荐答案

如果您知道sql,这更容易理解

If you know sql this is easier to understand

library(sqldf)
sqldf('select year, max(score) from mydata group by year')

更新(2016-01):现在您也可以使用dplyr

Update (2016-01): Now you can also use dplyr

library(dplyr)
mydata %>% group_by(year) %>% summarise(max = max(score))

这篇关于如何按组获取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆