[R] How to calculate mean of every nth time series data with zoo or xts ?

Rui Barradas ruipbarradas at sapo.pt
Sun Dec 2 19:30:24 CET 2012


Hello,

You should include a data example, using ?dput.
Anyway, making up some data,


t1 <- as.POSIXct("2012-1-1 0:0:0")
t2 <- as.POSIXct("2012-1-4 0:0:0")
d <- seq(t1, t2, by = "1 min")
x <- rnorm(length(d))

z <- zoo(x, d)

aggregate(z, list(format(index(z), "%H:%M")), FUN = mean)


Hope this helps,

Rui Barradas
Em 02-12-2012 18:09, 박상규 escreveu:
> Hello,
>
>
> I have 1-minute time series stock data and I'd like to calculate mean of every n-th candle data of m-days.
>
>
> result = c(mean of 1th data, mean of 2nd data, ...)
>
>
> mean of 1th data = (1th data of 2012-1-1 + 1th data of 2012-1-2 + 1th data of 2012-1-3) / 3
>
> mean of 2nd data = (2nd data of 2012-1-1 + 2nd data of 2012-1-2 + 2nd data of 2012-1-3) / 3
> ...
>
> Could you let me know the fastest method ?
>
>
>   Thanks in advance,
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list