我最初有15分钟的流量数据,看起来像这样
structure(list(t = structure(c(1136062800, 1136063700, 1136064600,
1136065500, 1136066400, 1136067300, 1136068200, 1136069100, 1136070000,
1136070900, 1136071800, 1136072700, 1136073600, 1136074500, 1136075400,
1136076300, 1136077200, 1136078100, 1136079000, 1136079900, 1136080800,
1136081700, 1136082600, 1136083500, 1136084400, 1136085300, 1136086200,
1136087100, 1136088000, 1136088900, 1136089800, 1136090700), class = c("POSIXct",
"POSIXt"), tzone = "EST"), flow = c(23, 31, 42, 59, 59, 59, 50,
48, 37, 33, 31, 31, 30, 30, 27, 27, 30, 31, 33, 37, 38, 42, 42,
48, 48, 46, 42, 38, 37, 35, 33, 35)), .Names = c("t", "flow"), row.names = 35003:35034, class = "data.frame")
我使用此代码将此数据剪切为2小时平均数据时间序列
data <- data.frame(t=streamflowDateTime,flow=streamflow)
data2hr <- data
data2hr$time <- cut(data2hr$t,breaks="2 hours")
smoothedData <- aggregate(flow~time,data2hr,mean)
现在我想扩展&#39; smoothedData&#39;通过使它成为每小时时间序列的时间序列,但我希望新的时间序列以2小时的间隔从smoothedData时间序列中保留其值,并且我希望将原始数据中的平均每小时数据插入到现有时间序列之间。救命啊!
答案 0 :(得分:0)
您可以使用函数approxfun
来实现此结果。
首先,让我们将time
中的smoothedData
列转换回POSIX时间格式:
smoothedData$time <- as.POSIXct(smoothedData$time)
# time flow
# 1 2005-12-31 16:00:00 46.375
# 2 2005-12-31 18:00:00 30.750
# 3 2005-12-31 20:00:00 37.625
# 4 2005-12-31 22:00:00 39.250
然后,创建一个小时分隔的时间序列:
data1hr <- data.frame(time=seq(min(smoothedData$time),max(smoothedData$time),by='1 hour'))
...制作插值函数,用于计算smoothedData
数据集的两小时间隔内的平均值:
interp.time <- approxfun(x=smoothedData$time,y=smoothedData$flow)
最后,将此函数应用于小时分隔的时间序列:
data1hr$flow <- interp.time(data1hr$time)
# time flow
# 1 2005-12-31 16:00:00 46.3750
# 2 2005-12-31 17:00:00 38.5625
# 3 2005-12-31 18:00:00 30.7500
# 4 2005-12-31 19:00:00 34.1875
# 5 2005-12-31 20:00:00 37.6250
# 6 2005-12-31 21:00:00 38.4375
# 7 2005-12-31 22:00:00 39.2500
修改强>
根据OP的评论,
我觉得我以一种比它更复杂的方式解释了我的问题 成为。基本上,在偶数小时时间戳,即00:00:00, 02:00:00我想要2小时平均值,但是在奇数小时时间戳 01:00:00,03:00:00我想要每小时平均值
我想提出另一个问题的解决方案:
data <- structure(list(t = structure(c(1136062800, 1136063700, 1136064600,
1136065500, 1136066400, 1136067300, 1136068200, 1136069100, 1136070000,
1136070900, 1136071800, 1136072700, 1136073600, 1136074500, 1136075400,
1136076300, 1136077200, 1136078100, 1136079000, 1136079900, 1136080800,
1136081700, 1136082600, 1136083500, 1136084400, 1136085300, 1136086200,
1136087100, 1136088000, 1136088900, 1136089800, 1136090700), class = c("POSIXct",
"POSIXt"), tzone = "EST"), flow = c(23, 31, 42, 59, 59, 59, 50,
48, 37, 33, 31, 31, 30, 30, 27, 27, 30, 31, 33, 37, 38, 42, 42,
48, 48, 46, 42, 38, 37, 35, 33, 35)), .Names = c("t", "flow"), row.names = 35003:35034, class = "data.frame")
data2hr <- data
data2hr$time <- cut(data2hr$t,breaks="2 hours")
smoothedData2hr <- aggregate(flow~time,data2hr,mean)
# time flow
# 1 2005-12-31 16:00:00 46.375
# 2 2005-12-31 18:00:00 30.750
# 3 2005-12-31 20:00:00 37.625
# 4 2005-12-31 22:00:00 39.250
data1hr <- data
data1hr$time <- cut(data1hr$t,breaks="1 hour")
smoothedData1hr <- aggregate(flow~time,data1hr,mean)
# time flow
# 1 2005-12-31 16:00:00 38.75
# 2 2005-12-31 17:00:00 54.00
# 3 2005-12-31 18:00:00 33.00
# 4 2005-12-31 19:00:00 28.50
# 5 2005-12-31 20:00:00 32.75
# 6 2005-12-31 21:00:00 42.50
# 7 2005-12-31 22:00:00 43.50
# 8 2005-12-31 23:00:00 35.00
result <- smoothedData1hr
result$flow[match(smoothedData2hr$time,result$time)] <- smoothedData2hr$flow
# time flow
# 1 2005-12-31 16:00:00 46.375
# 2 2005-12-31 17:00:00 54.000
# 3 2005-12-31 18:00:00 30.750
# 4 2005-12-31 19:00:00 28.500
# 5 2005-12-31 20:00:00 37.625
# 6 2005-12-31 21:00:00 42.500
# 7 2005-12-31 22:00:00 39.250
# 8 2005-12-31 23:00:00 35.000