I have a large file of measurements with 3-second period (here is a small part of it). I made time serie:

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)

On the next step I need convert it to the time series with 3 minutes update interval, where values must be the averages. What is the simplest way to do this?

有帮助吗?

解决方案

Use aggregate.zoo :

aggregate(dat, as.POSIXct(cut(index(dat), "3 min")), mean)

Note that dec="." and index=1 are used by default in read.zoo so they could be omitted from the read.zoo line.

其他提示

You can use the period.apply from the xts package

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

The second argument to period.apply is a vector of the row numbers of the end of each 3 minute period. endpoints(dat, "minutes", 3) calculates that for you.

If you want the timestamps to be "rounded" instead of being the last timestamp of each period, you can use align.time, but align.time requires that the object is an xts, so you'd have to convert to xts first.

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top