我有一个 xts
系列,其中包含一年的降水数据:
str(data_prec)
An ‘xts’ object from 2011-01-01 to 2011-12-31 23:55:00 containing:
Data: num [1:105125, 1] 0 0 0 0 0 0 0 0 0 0 ...
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
List of 2
$ tclass: chr [1:2] "POSIXct" "POSIXt"
$ tzone : chr ""
部分数据如下:
2011-12-15 05:15:00, 0
2011-12-15 05:20:00, 0
2011-12-15 05:25:00, 0.1
2011-12-15 05:30:00, 1.2
2011-12-15 05:31:00, 0.2
2011-12-15 05:32:00, 0.6
2011-12-15 05:33:00, 0.1
2011-12-15 05:35:00, 0.1
2011-12-15 05:36:00, 0
2011-12-15 05:37:00, 0.6
2011-12-15 05:40:00, 0
2011-12-15 05:45:00, 0
2011-12-15 05:50:00, 0.1
我需要每五分钟获取一次数据,对之前的数据进行求和。我尝试使用aggregate
、to.months5
和merge
但没有成功。我不知道我做错了什么。这是我达到的最接近的方法:
align.time(period.sum(data_prec,endpoints(data_prec,"minutes",k=5)),300)
这给了我:
2011-12-15 05:15:00, 0
2011-12-15 05:20:00, 0
2011-12-15 05:25:00, 0
2011-12-15 05:30:00, 0.1
2011-12-15 05:35:00, 2.1
2011-12-15 05:40:00, 0.7
2011-12-15 05:45:00, 0
2011-12-15 05:50:00, 0
2011-12-15 05:55:00, 0.1
2011-12-15 06:00:00, 0
这就是我正在寻找的:
2011-12-15 05:15:00, 0
2011-12-15 05:20:00, 0
2011-12-15 05:25:00, 0.1
2011-12-15 05:30:00, 1.2
2011-12-15 05:35:00, 1.0
2011-12-15 05:40:00, 0.6
2011-12-15 05:45:00, 0
2011-12-15 05:50:00, 0.1
2011-12-15 05:55:00, 0
2011-12-15 06:00:00, 0
感谢您的任何建议。
最佳答案
你对待时间的态度不一致。根据设计,一分钟的 :00 是该分钟的开始 - 例如12:00:00 属于 12:00:00 - 12:59:59.999999 范围,也称为第 12 小时。
因此,您需要将时间向后移动一小部分,以使其达到我认为您期望的那样。也就是说,您的“希望”结果也不一致(请参阅我的解决方案下面的添加内容):
解决方案
.index(x) <- .index(x) - 1
align.time(period.sum(x, endpoints(x,"mins",k=5)))
[,1]
2011-12-15 05:15:00 0.0
2011-12-15 05:20:00 0.0
2011-12-15 05:25:00 0.1
2011-12-15 05:30:00 1.2
2011-12-15 05:35:00 1.0
2011-12-15 05:40:00 0.6
2011-12-15 05:45:00 0.0
2011-12-15 05:50:00 0.1
您的问题
sum(data_prec) # the sample data you gave (well, not really gave in reproducible form)
[1] 3.0
# your addition
0.1 + 1.2 + 1 + 0.7 + .1
[1] 3.1
HTH
关于r - 将不规则降水时间序列转换为规则降水时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352793/