Hi, I got a little lost in your verbal description, but if I understand your code correctly (what is n?), I think I'd implement it with the split function: step i) make a single (hopefully vectorized) pass to get whatever split points you need (the offset is in this step); ii) use split() to break your data.frame into a list efficiently; iii) use lapply() to run your function on each element of the list.
Hope that helps, Michael On Wed, Feb 15, 2012 at 4:25 PM, Nerak <nera...@hotmail.com> wrote: > I saw I made a little mistake in the loop, > in the line > test.number$numberb[y-Year[1]+1]<-length(which(test.starty==1 & > test.f[(n+1)]== 1 )) > it is n+1 instead of n-1 (like I wrote in the beginning) > > But the question I have about it is still the same. > My excuzes > > > > Year<-data.frame(Date=c(1980,1980,1980,1980,1981,1981,1981,1981,1982,1982,1982,1982,1983,1983,1983,1983)) > test.b<-data.frame(C=c(0,0,0,0,5,2,0,0,0,15,12,10,6,0,0,0),B=c(0,0,0,0,9,6,2,0,0,24,20,16,2,0,0,0),F=c(0,0,0,0,6,5,1,0,0,18,16,12,10,5,1,0)) > test.start<-data.frame(C=c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0),B=c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0),F=c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0)) > > > > test.2b<-test.b>1 > test.number<-data.frame(c(1980:1983)) > for (l in 1:nrow(test.b)) > { > for (y in 1980:1983) > { > > test.f<-test.2b[which(Year == y),l] > test.starty<-test.start[which(Year ==y),l] > test.number$numberb[y-Year[1]+1]<-length(which(test.starty==1 & > test.f[(n+1)]== 1 )) > > } > test.number[,l+1]<-cbind(test.number$numberb) > > } > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/function-similar-to-ddply-calculations-based-on-previous-row-tp4390925p4392040.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.