Sorry... That last expression was backward... Wb30$ValidWB <- with( Wb30, 0 == cumsum( Water_Balance < 0 | 100 < Water_Balance ) )
--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnew...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On October 23, 2014 7:23:15 AM PDT, Jeff Newmiller <jdnew...@dcn.davis.ca.us> wrote: >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 you should be trying to understand how your data are >invalid so you can figure out if you can extract any meaning from it. >Drift? Random (lots of ways... which)? Offset? Marking the bad records >in a new status column is a start... blindly proceeding to pretend as >though there were no problem in the data is deceptive. > >Once you get the input data cleaned up so you don't need to butcher the >output, the R way to do this is > >Wb30$Water_Balance <- with( Wb30, cumsum( Rainfall - Evaporation ) ) >Wb30$ValidWB <- with( Wb30, 0 == cumsum( 0 <= Water_Balance & >Water_Balance <= 100 ) ) > >--------------------------------------------------------------------------- >Jeff Newmiller The ..... ..... Go >Live... >DCN:<jdnew...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live >Go... > Live: OO#.. Dead: OO#.. Playing >Research Engineer (Solar/Batteries O.O#. #.O#. with >/Software/Embedded Controllers) .OO#. .OO#. >rocks...1k >--------------------------------------------------------------------------- > >Sent from my phone. Please excuse my brevity. > >On October 23, 2014 6:35:27 AM PDT, Frederic Ntirenganya ><ntfr...@gmail.com> wrote: >>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){ >> w=0 >> } >> >> wb=Wb30$Water_Balance[w-1] + Wb30$Rainfall[w]-Wb30$Evaporation[w] >>} >>wb >> >>Regards, >>Frederic. >> >>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/ >> >>On Thu, Oct 23, 2014 at 3:44 PM, Duncan Murdoch >><murdoch.dun...@gmail.com> >>wrote: >> >>> 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. >>> > >>> >>> >> >> [[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. ______________________________________________ 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.