【问题标题】:How can I add the values of a column based on a time interval of a day/month/year?如何根据天/月/年的时间间隔添加列的值?
【发布时间】:2016-03-17 13:46:18
【问题描述】:

我能够使用以下方法添加每天(12:00 AM - 12:00 AM)的降水数据:

 pptnew<-data        
 pptnew<-aggregate(.~timestampDMY,data=pptnew, FUN = sum)
 #timestampDMY is a factor with Date Month and Year Values (made a factor      
 from POSIXlt using format argument to get rid of minutes/seconds) because 
 aggregate probably doesnot work on POSIXlt class.

但是,现在我想根据时间间隔添加列的值(即以毫米/30 分钟为单位的降水量) - 假设一天的上午 9:00 到第二天的上午 9:00 .

以下是一个虚拟数据集(download):

  Date_Time      Rainfall_mm
17-05-2013 19:00:00 0.8
17-05-2013 19:30:00 0.5
17/05/2013 20:00:00 0.3
17/05/2013 20:30:00 0
17/05/2013 21:00:00 0
17/05/2013 21:30:00 0
17/05/2013 22:00:00 0
17/05/2013 22:30:00 0
17/05/2013 23:00:00 0
17/05/2013 23:30:00 0
17/05/2013 00:00:00 1.3
18/05/2013 00:30:00 1.5
18/05/2013 01:00:00 1.7
18/05/2013 01:30:00 1.9
18/05/2013 02:00:00 2.1
18/05/2013 02:30:00 2.3
18/05/2013 03:00:00 2.5
18/05/2013 03:30:00 2.7
18/05/2013 04:00:00 2.9
18/05/2013 04:30:00 3.1
18/05/2013 05:00:00 3.3
18/05/2013 05:30:00 0
18/05/2013 06:00:00 0
18/05/2013 06:30:00 0
18/05/2013 07:00:00 0
18/05/2013 07:30:00 0
18/05/2013 08:00:00 0
18/05/2013 08:30:00 0
18/05/2013 09:00:00 0
18/05/2013 09:30:00 0
18/05/2013 10:00:00 0.6
18/05/2013 10:30:00 0.8
18/05/2013 11:00:00 0.5
18/05/2013 11:30:00 0
18/05/2013 12:00:00 0
18/05/2013 12:30:00 0
18/05/2013 13:00:00 0
18/05/2013 13:30:00 0
18/05/2013 14:00:00 0
18/05/2013 14:30:00 0
18/05/2013 15:00:00 0
18/05/2013 15:30:00 0
18/05/2013 16:00:00 0
18/05/2013 16:30:00 0
18/05/2013 17:00:00 0
18/05/2013 17:30:00 0
18/05/2013 18:00:00 0
18/05/2013 18:30:00 0
18/05/2013 19:00:00 0
18/05/2013 19:30:00 0
18/05/2013 20:00:00 0.3
18/05/2013 20:30:00 0.3
18/05/2013 21:00:00 0.7
18/05/2013 21:30:00 0.9
18/05/2013 22:00:00 1
18/05/2013 22:30:00 1
18/05/2013 23:00:00 
18/05/2013 23:30:00 
18/05/2013 00:00:00 
19/05/2013 00:30:00 
19/05/2013 01:00:00 
19/05/2013 01:30:00 
19/05/2013 02:00:00 
19/05/2013 02:30:00 
19/05/2013 03:00:00 
19/05/2013 03:30:00 
19/05/2013 04:00:00 0
19/05/2013 04:30:00 0
19/05/2013 05:00:00 0
19/05/2013 05:30:00 0.2
19/05/2013 06:00:00 0.2
19/05/2013 06:30:00 0.4
19/05/2013 07:00:00 0.7
19/05/2013 07:30:00 0.1
19/05/2013 08:00:00 0
19/05/2013 08:30:00 0
19/05/2013 09:00:00 0
19/05/2013 09:30:00 0
19/05/2013 10:00:00 0
19/05/2013 10:30:00 0
19/05/2013 11:00:00 0
19/05/2013 11:30:00 0
19/05/2013 12:00:00 0
19/05/2013 12:30:00 0
19/05/2013 13:00:00 0
19/05/2013 13:30:00 0
19/05/2013 14:00:00 0
19/05/2013 14:30:00 0
19/05/2013 15:00:00 0
19/05/2013 15:30:00 0.8
19/05/2013 16:00:00 0.5
19/05/2013 16:30:00 0.3
19/05/2013 17:00:00 0
19/05/2013 17:30:00 0
19/05/2013 18:00:00 0
19/05/2013 18:30:00 0
19/05/2013 19:00:00 0
19/05/2013 19:30:00 0
19/05/2013 20:00:00 0
19/05/2013 20:30:00 1.3
19/05/2013 21:00:00 1.5
19/05/2013 21:30:00 1.7
19/05/2013 22:00:00 1.9
19/05/2013 22:30:00 2.1
19/05/2013 23:00:00 2.3
19/05/2013 23:30:00 2.5
19/05/2013 00:00:00 2.7
20/05/2013 00:30:00 2.9
20/05/2013 01:00:00 3.1
20/05/2013 01:30:00 3.3
20/05/2013 02:00:00 0
20/05/2013 02:30:00 0
20/05/2013 03:00:00 0
20/05/2013 03:30:00 0
20/05/2013 04:00:00 0
20/05/2013 04:30:00 0
20/05/2013 05:00:00 0
20/05/2013 05:30:00 0
20/05/2013 06:00:00 0
20/05/2013 06:30:00 0.6
20/05/2013 07:00:00 0.8
20/05/2013 07:30:00 0.5
20/05/2013 08:00:00 0
20/05/2013 08:30:00 0
20/05/2013 09:00:00 0
20/05/2013 09:30:00 0
20/05/2013 10:00:00 0
20/05/2013 10:30:00 0
20/05/2013 11:00:00 0
20/05/2013 11:30:00 0
20/05/2013 12:00:00 0
20/05/2013 12:30:00 0
20/05/2013 13:00:00 0
20/05/2013 13:30:00 0
20/05/2013 14:00:00 0
20/05/2013 14:30:00 0
20/05/2013 15:00:00 0
20/05/2013 15:30:00 0
20/05/2013 16:00:00 0
20/05/2013 16:30:00 0.3
20/05/2013 17:00:00 0.3
20/05/2013 17:30:00 0.7
20/05/2013 18:00:00 0.9
20/05/2013 18:30:00 1
20/05/2013 19:00:00 1
20/05/2013 19:30:00 
20/05/2013 20:00:00 
20/05/2013 20:30:00 
20/05/2013 21:00:00 
20/05/2013 21:30:00 
20/05/2013 22:00:00 
20/05/2013 22:30:00 
20/05/2013 23:00:00 
20/05/2013 23:30:00 
20/05/2013 00:00:00 
21/05/2013 00:30:00 0
21/05/2013 01:00:00 0
21/05/2013 01:30:00 0
21/05/2013 02:00:00 0.2
21/05/2013 02:30:00 0.2
21/05/2013 03:00:00 0.4
21/05/2013 03:30:00 0.7
21/05/2013 04:00:00 0.1
21/05/2013 04:30:00 0
21/05/2013 05:00:00 0
21/05/2013 05:30:00 0
21/05/2013 06:00:00 0
21/05/2013 06:30:00 0
21/05/2013 07:00:00 0
21/05/2013 07:30:00 0
21/05/2013 08:00:00 0
21/05/2013 08:30:00 0
21/05/2013 09:00:00 0
21/05/2013 09:30:00 0
21/05/2013 10:00:00 0
21/05/2013 10:30:00 0
21/05/2013 11:00:00 0
21/05/2013 11:30:00 0
21/05/2013 12:00:00 0.8
21/05/2013 12:30:00 0.5
21/05/2013 13:00:00 0.3
21/05/2013 13:30:00 0
21/05/2013 14:00:00 0
21/05/2013 14:30:00 0
21/05/2013 15:00:00 0
21/05/2013 15:30:00 0
21/05/2013 16:00:00 0
21/05/2013 16:30:00 0
21/05/2013 17:00:00 1.3
21/05/2013 17:30:00 1.5
21/05/2013 18:00:00 1.7
21/05/2013 18:30:00 1.9
21/05/2013 19:00:00 2.1
21/05/2013 19:30:00 2.3
21/05/2013 20:00:00 2.5
21/05/2013 20:30:00 2.7
21/05/2013 21:00:00 2.9
21/05/2013 21:30:00 3.1
21/05/2013 22:00:00 3.3
21/05/2013 22:30:00 0
21/05/2013 23:00:00 0
21/05/2013 23:30:00 0
21/05/2013 00:00:00 0
22/05/2013 00:30:00 0
22/05/2013 01:00:00 0
22/05/2013 01:30:00 0
22/05/2013 02:00:00 0

提前谢谢你。 (请注意可能会缺少一天的值)。

问候 满足

【问题讨论】:

  • 显然你想计算一个移动/滚动和。 Zoo 包为此提供了一个功能。
  • 谢谢,但我不知道如何为此使用滚动总和?我需要从一天上午 9:00 到第二天上午 9:00 的总降雨量。

标签: r time-series aggregate data-manipulation


【解决方案1】:

您是否知道您的数据集中有多个缺失的数据点?

library(dplyr)
library(tidyr)
df <- separate(df, Date_Time, c("dmyVar", "hmsVar"), sep = " ")

# see unique values available
unique(df$hmsVar)

# aggregate by timestamp, as you requested
df %>%
  group_by(hmsVar) %>%
  summarise(sumByHour = sum(Rainfall_meas_mm, na.rm = TRUE))
Source: local data frame [48 x 2]

     hmsVar sumByHour
      (chr)     (dbl)
1  00:00:00       4.0
2  00:30:00       4.4
3  01:00:00       4.8
4  01:30:00       5.2
5  02:00:00       2.3
6  02:30:00       2.5
7  03:00:00       2.9
8  03:30:00       3.4
9  04:00:00       3.0
10 04:30:00       3.1
..      ...       ...

【讨论】:

  • 感谢您的回答。不过,我想我还不是很清楚。在您给我的代码中,它正在计算每个时间点的总降雨量,而不是 9 小时到 9 小时的间隔。我想根据时间间隔添加降雨值(每​​ 30 分钟记录一次),从一天的 0900 小时到第二天的 0900 小时,而不是午夜到午夜。午夜到午夜对我来说很容易做到,因为我可以根据日期汇总它们。但我无法根据 9 到 9 来汇总它们,因为日期会发生变化。 .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多