Hi R users, I need your help to write a code in r that does the following calculation from three different datasets; ac = 1/sum (NPP from date 1 to date 2, dataset=1) * (biomass at date 2 -biomass at date 1, dataset = 2) + (littfall at date 2, dataset=3). all the dates are in yr-month-day format. Which library or function Should I use to tell R do these calculations of these variables at different dates. I appreciate your help. Ahmed Attia, Ph.D. Agronomist & Soil Scientist
We expect posters to have made an effort to learn R and show us code that they have tried. We do not provide free software development. The "Introduction to R" tutorial that ships with the software is one place to start, but there are many good web tutorials also available. Just search. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Jun 21, 2017 at 11:19 AM, Ahmed Attia <ahmedatia80 at gmail.com> wrote:> Hi R users, > > I need your help to write a code in r that does the following > calculation from three different datasets; > > ac = 1/sum (NPP from date 1 to date 2, dataset=1) * (biomass at date 2 > -biomass at date 1, dataset = 2) + (littfall at date 2, dataset=3). > > all the dates are in yr-month-day format. Which library or function > Should I use to tell R do these calculations of these variables at > different dates. > > I appreciate your help. > > Ahmed Attia, Ph.D. > Agronomist & Soil Scientist > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hi Ahmed, Your problem appears trivial as you have already specified the form of the calculation. Learn how to "extract" specified elements from a data structure: # first value sum(dataset1$NPP[dataset1$date >= date1 & dataset1$date <= date2]) # second value dataset2$biomass[dataset2$date == date2] - dataset2$biomass[dataset2$date == date1] # third value dataset3$littfall[dataset3$date == date2] Note that you may have to convert character strings to dates to do the above - see a function like "as.Date". Obviously I do not know the actual names of your datasets and I am assuming that the variable names you have given are the actual ones. Jim On Thu, Jun 22, 2017 at 4:19 AM, Ahmed Attia <ahmedatia80 at gmail.com> wrote:> Hi R users, > > I need your help to write a code in r that does the following > calculation from three different datasets; > > ac = 1/sum (NPP from date 1 to date 2, dataset=1) * (biomass at date 2 > -biomass at date 1, dataset = 2) + (littfall at date 2, dataset=3). > > all the dates are in yr-month-day format. Which library or function > Should I use to tell R do these calculations of these variables at > different dates. > > I appreciate your help. > > Ahmed Attia, Ph.D. > Agronomist & Soil Scientist > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
> On Jun 21, 2017, at 11:19 AM, Ahmed Attia <ahmedatia80 at gmail.com> wrote: > > Hi R users, > > I need your help to write a code in r that does the following > calculation from three different datasets; > > ac = 1/sum (NPP from date 1 to date 2, dataset=1) * (biomass at date 2 > -biomass at date 1, dataset = 2) + (littfall at date 2, dataset=3).To do this in R you could learn to use the "sqldf"-package since you seem familiar with that syntax. I'm not and so it's rank speculation on my part that such an expression would be meaningful in one or another flavor of SQL. Or you could use either `match` or `merge`, assuming the dates of the various datasets are congruent.> > all the dates are in yr-month-day format.The "YYYY-MM-DD" format is the standard format in R. You might not even need to convert to the Date-class.> Which library or function > Should I use to tell R do these calculations of these variables at > different dates. > > I appreciate your help. > > Ahmed Attia, Ph.D. > Agronomist & Soil Scientist > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA
Hi Jim, Thank you very much, this was so helpful. GPP_Ahmed13$Date <- as.Date(GPP_Ahmed13$Date, '%Y/%m/%d') Litterfall_Ahmed97$Date <- as.Date(Litterfall_Ahmed97$Date, '%Y/%m/%d') leafbiom97$Date <- as.Date( leafbiom97$Date, '%Y/%m/%d') (leafbiom97$LeafBiog[leafbiom97$Date == "2012-02-12"] - leafbiom97$LeafBiog[leafbiom97$Date == "2010-03-15"]+ Litterfall_Ahmed97$littperiod[Litterfall_Ahmed97$Date =="2011-04-08"])/ (sum(GPP_Ahmed13$GPP[GPP_Ahmed13$Date >= "2010-03-12" & GPP_Ahmed13$Date <= "2012-04-12"])/2) Best regards Ahmed Ahmed Attia, Ph.D. Agronomist & Soil Scientist On Thu, Jun 22, 2017 at 12:24 AM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Ahmed, > Your problem appears trivial as you have already specified the form of > the calculation. > > Learn how to "extract" specified elements from a data structure: > > # first value > sum(dataset1$NPP[dataset1$date >= date1 & > dataset1$date <= date2]) > # second value > dataset2$biomass[dataset2$date == date2] - > dataset2$biomass[dataset2$date == date1] > # third value > dataset3$littfall[dataset3$date == date2] > > Note that you may have to convert character strings to dates to do the > above - see a function like "as.Date". Obviously I do not know the > actual names of your datasets and I am assuming that the variable > names you have given are the actual ones. > > Jim > > > On Thu, Jun 22, 2017 at 4:19 AM, Ahmed Attia <ahmedatia80 at gmail.com> wrote: >> Hi R users, >> >> I need your help to write a code in r that does the following >> calculation from three different datasets; >> >> ac = 1/sum (NPP from date 1 to date 2, dataset=1) * (biomass at date 2 >> -biomass at date 1, dataset = 2) + (littfall at date 2, dataset=3). >> >> all the dates are in yr-month-day format. Which library or function >> Should I use to tell R do these calculations of these variables at >> different dates. >> >> I appreciate your help. >> >> Ahmed Attia, Ph.D. >> Agronomist & Soil Scientist >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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.