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.