我一直在試圖通過查看其他帖子來執行此操作,但我一直收到錯誤消息。我的數據new
看起來是這樣的:按組選擇最大行值
id year name gdp
1 1980 Jamie 45
1 1981 Jamie 60
1 1982 Jamie 70
2 1990 Kate 40
2 1991 Kate 25
2 1992 Kate 67
3 1994 Joe 35
3 1995 Joe 78
3 1996 Joe 90
我想選擇與ID最高的年份值的行。所以想要的輸出是:
id year name gdp
1 1982 Jamie 70
2 1992 Kate 67
3 1996 Joe 90
從Selecting Rows which contain daily max value in R我嘗試以下,但沒有奏效
ddply(new,~id,function(x){x[which.max(new$year),]})
我也試過
tapply(new$year, new$id, max)
但這並沒有給我想要的輸出。
任何建議將真的幫助!
說實話,這似乎過於複雜這個任務。你基本上用'split' +'lapply'來重新創建'by'' – thelatemail