根据dplyr中另一个字段的值来总结一个字段 [英] summarizing a field based on the value of another field in dplyr

查看:223
本文介绍了根据dplyr中另一个字段的值来总结一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含四个字段的数据框架DF: id,date,feature,value 。我想生成一个包含三个字段的数据框架DF2: id,feature,value ,其中是值对于最新可用的日期 c> c c> c c> c < 。在plyr的说法:

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)]))

我是有一点亏损,如何用dplyr使用group_by和总结来实现这一点。

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

推荐答案

这只是你的直接翻译plyr在dplyr中调用:

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))])

这篇关于根据dplyr中另一个字段的值来总结一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆