数据:- 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 Balance1 John 2016 1002 John 2015 1503 Stacy 2014 654 Stacy 2016 755 Kat 2006 1506 K
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这两个列来汇总/汇总上面的数据框.我使用基函数聚合来做到这一点.我需要最近一年/最近一年的最大余额.在输出的第一行,约翰有最新的一年(2016),但是(2015)的余额,这不是我需要的,它应该输出100而不是150.我在哪里错了?
具有讽刺意味的是,聚合是一种糟糕的聚合工具.你可以让它工作,但我会这样做:library(data.table) setDT(df)[order(-Year, -Balance), .SD[1], by = Name] # Name Year Balance #1: John 2016 100 #2: Stacy 2016 75 #3: Kat 2006 150