[R] Computing Water Balance using a loop.

Frederic Ntirenganya ntfredo at gmail.com
Thu Oct 23 14:33:31 CEST 2014


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]]



More information about the R-help mailing list