Re: [R] Computing Water Balance using a loop.

2014-10-24 Thread Frederic Ntirenganya
Thanks All, This is how I solved the problem and working correctly. ndays <- nrow(Wb30) for (iday in 2:ndays) { #Wb30$Water_Balance <- with( Wb30, cumsum(Water_Balance + Rainfall - Evaporation ) ) # Wb30$Water_Balance <- with( Wb30, cumsum(Wb30$Water_Balance[iday-1] + Wb30$Rainfall[i

Re: [R] Computing Water Balance using a loop.

2014-10-24 Thread Frederic Ntirenganya
Hi Mac, The first entry is 0 for water balance. That means the 3rd should be zero according to the formula. Water balance today = Water balance yesterday + Rainfall āˆ’ Evaporation This loop gives 5 for each date. I don't see why ? May Rainfall Evaporation Water_Balance 1 70

Re: [R] Computing Water Balance using a loop.

2014-10-23 Thread MacQueen, Don
If I understand the problem correctly, then I’d suggest this: ndays <- nrow(Wb30) for (iday in 2:ndays) { Wb30$Water_Balance[iday] <- Wb30$Water_Balance[iday-1] + Wb30$Rainfall[iday] - Wb30$Evaporation[iday] Wb30$Water_Balance[iday] <- min(Wb30$Water_Balance[iday], 100) Wb30$Water_Ba

Re: [R] Computing Water Balance using a loop.

2014-10-23 Thread Jeff Newmiller
Sorry... That last expression was backward... Wb30$ValidWB <- with( Wb30, 0 == cumsum( Water_Balance < 0 | 100 < Water_Balance ) ) --- Jeff NewmillerThe . . Go Live... DCN:B

Re: [R] Computing Water Balance using a loop.

2014-10-23 Thread Jeff Newmiller
Counting chickens after they have left the coop is not going to work. If your inputs push w outside the limits of physics then your input data are invalid. Arbitrarily forcing w to fit in that case partially ignores the inputs anyway... and since there are many ways for the data to be invalid yo

Re: [R] Computing Water Balance using a loop.

2014-10-23 Thread Frederic Ntirenganya
Dear Duncan, Those condition should be there and also look at Rainfall and evaporation columns. If i change it to be like the following loop, it can't do it. The problem is how to include those conditions and also respect the formula? wb=c() for (w in 1:length(Wb30$Water_Balance)){ if(w<0){

Re: [R] Computing Water Balance using a loop.

2014-10-23 Thread Duncan Murdoch
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_B