[R] How to calculate mean of every nth time series data with zoo or xts ?
arun
smartpink111 at yahoo.com
Sun Dec 2 20:12:06 CET 2012
Hi,
You could use:
set.seed(5)
dat1<-data.frame(Date=seq(from=as.POSIXct("2012-11-01 00:00:00"),to=as.POSIXct("2012-11-03 23:59:00"), by="1 min"),col2=rnorm(4320,0,1))
library(xts)
dat2<-xts(dat1[,-1],order.by=dat1[,1])
res<-ave(dat2[,1],format(index(dat2),"%H:%M"),FUN=mean)
#or
res<-tapply(dat2[,1],format(index(dat2),"%H:%M"),FUN=mean)
head(res)
# 00:00 00:01 00:02 00:03 00:04 00:05
#-0.50875473 0.07165023 -0.35019421 0.44377065 0.61380169 0.21490898
A.K.
----- Original Message -----
From: 박상규 <birdfire94 at naver.com>
To: r-help at r-project.org
Cc:
Sent: Sunday, December 2, 2012 1:09 PM
Subject: [R] How to calculate mean of every nth time series data with zoo or xts ?
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