Hi all,

I'm usually comfortable using the *apply functions for vectorizing loops 
in R. However, my particular problem now is using it in a sequential 
operation, which uses values evaluated in an offset of the loop vector. 
Here is my example using a for loop approach:

dat <- data.frame(year=rep(1970:1980,each=365),yday=1:365)
dat$value <- sin(dat$yday*2*pi/365)+rnorm(nrow(dat),sd=0.5)
dat$ca <- dat$cb <- 0 # consecutive above and below 0

for(n in 2:nrow(dat)){
  if(dat$value[n] > 0)
    dat$ca[n] <- dat$ca[n-1] + 1
  else
    dat$cb[n] <- dat$cb[n-1] + 1
}

I'm inquiring if there is a straightforward way to vectorize this (or a 
similar example) in R, since it gets rather slow with larger data 
frames. If there is no straightforward method, no worries.

Thanks in advance.
+mt

______________________________________________
[email protected] 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