我有一个大的测量文件,周期为3秒(here只是其中的一小部分)。我订了时间系列:
library(zoo)
fmt<-"%d.%m.%y %H:%M:%S"
dat <- read.zoo("~/Institut/Monitor/UA_test.csv",header=TRUE,
dec=".",sep='\t',tz='',format=fmt,index=1)
在下一步中,我需要将其转换为3分钟更新间隔的时间序列,其中值必须是平均值。最简单的方法是什么?
答案 0 :(得分:0)
使用aggregate.zoo
:
aggregate(dat, as.POSIXct(cut(index(dat), "3 min")), mean)
请注意,dec="."
默认使用index=1
和read.zoo
,因此可以从read.zoo
行中省略它们。
答案 1 :(得分:0)
您可以使用period.apply
包
xts
library(xts)
(x <- period.apply(dat, endpoints(dat, "minutes", 3), mean))
# UA UB UC
#2014-04-13 00:59:57 209.1605 226.4110 213.7115
#2014-04-13 01:02:57 215.4467 226.7065 211.3325
#2014-04-13 01:05:57 216.6252 225.4948 214.1290
#2014-04-13 01:07:45 218.4633 219.5589 214.1325
period.apply
的第二个参数是每3分钟结束时行数的向量。 endpoints(dat, "minutes", 3)
为您计算。
如果您希望时间戳“舍入”而不是每个句点的最后一个时间戳,则可以使用align.time
,但align.time
要求对象为xts
,所以你必须先转换为xts
。
xx <- as.xts(x)
align.time(xx, n=60*3)
# UA UB UC
#2014-04-13 01:00:00 209.1605 226.4110 213.7115
#2014-04-13 01:03:00 215.4467 226.7065 211.3325
#2014-04-13 01:06:00 216.6252 225.4948 214.1290
#2014-04-13 01:09:00 218.4633 219.5589 214.1325