On 23/10/2014, 8:33 AM, Frederic Ntirenganya wrote: > Dear All, > > I want to calculate water balance using the following formula: > Water balance today = Water balance yesterday + Rainfall − Evaporation > > This is a sample of data I am using: > > head(Wb30) > May Rainfall Evaporation Water_Balance > 1 7 0 5 0 > 2 8 10 5 NA > 3 9 0 5 NA > 4 10 0 5 NA > 5 11 2 5 NA > 6 12 23 5 NA > > The following is the loop am trying to use but it is not working well. > > #Water balance today = Water balance yesterday + Rainfall − Evaporation > #If Water balance today < 0 then Water balance today = 0 > #If Water balance today > 100 then Water balance today = 100 > wb=c() > for (w in 1:length(Wb30$Water_Balance)){ > if(w<0 & w>100){w<-0 & w<-100}
The line above doesn't make sense. It is impossible for w to be both less than 0 and greater than 100, so the condition will never be true. And if it is true, "w<-0 & w<-100" is not a sensible thing to do. > #print (w); > for (i in 1:length(Wb30$Rainfall)){ > for (j in 1:length(Wb30$Evaporation)){ > } > } Those loops do nothing. > wb<-Wb30$Water_Balance[w] + Wb30$Rainfall[i+1]-Wb30$Evaporation[j+1] Those should be using w-1 in place of w, and w in place of i+1 and j+1. Duncan Murdoch > } > wb > > Any suggest of what I am missing for it to work correctly is welcome. > > > Regards, > > Frederic Ntirenganya > Maseno University, > African Maths Initiative, > Kenya. > Mobile:(+254)718492836 > Email: fr...@aims.ac.za > https://sites.google.com/a/aims.ac.za/fredo/ > > [[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.