박상규
2012-Dec-02 18:09 UTC
[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]]
Rui Barradas
2012-Dec-02 18:30 UTC
[R] How to calculate mean of every nth time series data with zoo or xts ?
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.
arun
2012-Dec-02 19:12 UTC
[R] How to calculate mean of every nth time series data with zoo or xts ?
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.