On Jun 15, 2010, at 1:12 AM, James McCreight wrote:

Hi R'ers-

I'll believe this can be done and that I'm just not clever enough.

I'm trying to do this in 2D, but i think we have the same problem in 1D.

#Assume you have some 1D time series
d<-rnorm(100)

#Say you want to get the average over intervals of different lengths
#like from time 10 to time 24 and time 51 to time 86
starts <- c(10,51)
ends <- c(24,86)
results <-c(NA,NA)

#then you'd typically loop
for (i in 1:2) { results[i] <- mean(d[starts[i]:ends[i]) }

can it be done without looping?

Depends on what you mean by "without looping":

> mapply(function(x,y,z){mean(z[x:y])} ,x=starts, y=ends, MoreArgs=list(d))
[1] -0.219429514 -0.008565724

Probably no more, and quite possibly less, efficient than a for-loop.


Thanks in advance.

James

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to