select maximum row value by group

前端 未结 5 1153
醉梦人生
醉梦人生 2021-01-20 07:53

I\'ve been trying to do this with my data by looking at other posts, but I keep getting an error. My data new looks like this:

id  year    name          


        
5条回答
  •  日久生厌
    2021-01-20 07:55

    Your ddply effort looks good to me, but you referenced the original dataset in the callback function.

    ddply(new,~id,function(x){x[which.max(new$year),]})
    # should be
    ddply(new,.(id),function(x){x[which.max(x$year),]})
    

提交回复
热议问题