[R] Computing Water Balance using a loop.

Frederic Ntirenganya ntfredo at gmail.com
Fri Oct 24 11:54:48 CEST 2014


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[iday] - Wb30$Evaporation[iday] ) )
    #Wb30$Water_Balance[iday] <- min(Wb30$Water_Balance[iday], 0)
    #Wb30$Water_Balance[iday] <- max(Wb30$Water_Balance[iday], 100)
    #Wb30$ValidWB <- with( Wb30, 0 == cumsum( Water_Balance < 0 | 100 <
Water_Balance ) )
    Wb30$Water_Balance[iday] <- Wb30$Water_Balance[iday-1] +
Wb30$Rainfall[iday] - Wb30$Evaporation[iday]
    if (Wb30$Water_Balance[iday]<0){
      Wb30$Water_Balance[iday]=0
    }else if(Wb30$Water_Balance[iday]>100){
      Wb30$Water_Balance[iday]=100
    }
  }

Regards,
Frederic.

Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fredo at aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/

On Fri, Oct 24, 2014 at 10:49 AM, Frederic Ntirenganya <ntfredo at gmail.com>
wrote:

> 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   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
>
> Regards,
> Frederic.
>
> Frederic Ntirenganya
> Maseno University,
> African Maths Initiative,
> Kenya.
> Mobile:(+254)718492836
> Email: fredo at aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
>
> On Thu, Oct 23, 2014 at 7:17 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:
>
>> 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_Balance[iday] <- max(Wb30$Water_Balance[iday], 0)
>>
>> }
>>
>> It may be possible without a loop, but I think a loop is easier to
>> understand in the current circumstances. And is likely to be fast enough
>> that it does not matter.
>>
>> -
>> Don MacQueen
>>
>> Lawrence Livermore National Laboratory
>> 7000 East Ave., L-627
>> Livermore, CA 94550
>> 925-423-1062
>>
>>
>>
>>
>>
>> On 10/23/14, 5:33 AM, "Frederic Ntirenganya" <ntfredo at gmail.com> 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}
>> >  #print (w);
>> > for (i in 1:length(Wb30$Rainfall)){
>> >    for (j in 1:length(Wb30$Evaporation)){
>> >    }
>> >  }
>> > wb<-Wb30$Water_Balance[w] + Wb30$Rainfall[i+1]-Wb30$Evaporation[j+1]
>> >}
>> >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: fredo at aims.ac.za
>> >https://sites.google.com/a/aims.ac.za/fredo/
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> >______________________________________________
>> >R-help at 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.
>>
>>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list