summarizing a field based on the value of another

2019-09-23 13:49发布

问题:

I have a data frame DF with four fields: id, date, feature, value. I would like to generate a data frame DF2 with three fields: id, feature, value, where value is the value for the corresponding id and feature for the latest available date. In plyr parlance:

DF2 <- ddply(DF, .(id, feature), function(x) c(value(x$value[x$date == max(x$date)]))

I am a bit at a loss on how to achieve this with dplyr using group_by and summarize.

回答1:

This is just a direct translation of your plyr call in dplyr:

library(dplyr)
DF2 = summarise(group_by(DF, id, feature), value=value[which(date == max(date))])


标签: r dplyr