R按日期分组,并汇总值 [英] R group by date, and summarize the values

查看:281
本文介绍了R按日期分组,并汇总值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R对我来说是新手,我正在使用(私有)数据集。

R is new for me and I am working with a (private) data set.

我有以下问题,但时间序列很多:

I have the following problem, I have a lot of time series:

2015-04-27  12:29:48
2015-04-27  12:31:48
2015-04-27  12:34:50
2015-04-27  12:50:43
2015-04-27  12:53:55
2015-04-28  00:00:00
2015-04-28  00:00:10

所有时间序列都有值:

Datetime                   value
2015-04-27  12:29:48       0.0 
2015-04-27  12:31:48       0.0
2015-04-27  12:34:50       1.1
2015-04-27  12:50:43      45.0 
2015-04-27  12:53:55       0.0
2015-04-28  00:00:00       1.0
2015-04-28  00:00:10       2.0

我想跳过所有的小时和分钟,并像这样将它们总计起来:

I want to skip all the hours and minutes, and sum it all together like this:

Datetime      value
2015-04-27    46.1
2015-04-28     3.0

我做的第一件事是transfo rm列datetime:

The first thing i did was transform the column datetime:

energy$datetime <- as.POSIXlt(energy$datetime)  

我尝试了汇总功能:

df %>% group_by(energy$datetime) %>% summarize (energy$newname(energy$value))

但这不起作用。

我还在互联网上阅读了一些有竞争力的资料(例如: http://r.789695.n4.nabble.com/How-to-sum-and-将数据分组到数据帧中的日期td903708.html ),但对我来说这没有意义。

I also read competitive stuff on the internet (e.g.: http://r.789695.n4.nabble.com/How-to-sum-and-group-data-by-DATE-in-data-frame-td903708.html) but it doesn't make sense to me.

我该如何解决此问题吗?

How can I fix this issue?

推荐答案

使用as.Date()然后再聚合()。

Use as.Date() then aggregate().

energy$Date <- as.Date(energy$Datetime)
aggregate(energy$value, by=list(energy$Date), sum)



EDIT



Emma提出了一个很好的观点列名。您可以通过使用以下内容来保留聚合中的列名称。

EDIT

Emma made a good point about column names. You can preserve column names in aggregate by using the following instead.

aggregate(energy["value"], by=energy["Date"], sum)

这篇关于R按日期分组,并汇总值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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