[英]How can I add the values of a column based on a time interval of a day/month/year?
我能够使用以下方法每天(大约上午 12:00 - 上午 12:00)添加降水数据:
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。
以下是一个虚拟数据集( 下载):
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
先感谢您。 (请注意,一天中可能缺少值)。
问候 Sate
您是否知道数据集中有多个缺失的数据点?
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
.. ... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.