【发布时间】:2016-09-07 12:17:15
【问题描述】:
数据:-
df=data.frame(Name=c("John","John","Stacy","Stacy","Kat","Kat"),Year=c(2016,2015,2014,2016,2006,2006),Balance=c(100,150,65,75,150,10))
Name Year Balance
1 John 2016 100
2 John 2015 150
3 Stacy 2014 65
4 Stacy 2016 75
5 Kat 2006 150
6 Kat 2006 10
代码:-
aggregate(cbind(Year,Balance)~Name,data=df,FUN=max )
输出:-
Name Year Balance
1 John 2016 150
2 Kat 2006 150
3 Stacy 2016 75
我想使用 Year 和 Balance 两列来聚合/汇总上述数据框。我使用基本函数 aggregate 来执行此操作。我需要最近一年/最近一年的最大余额。输出的第一行,John 有最近的一年 (2016) 但余额是 (2015) ,这不是我需要的,它应该输出 100 而不是 150。我在哪里出错了?
【问题讨论】:
标签: r aggregate summarization