Hello R users,
The code below is for loop in R that I want to do to following
calculation at each time i and i-1 in 2:75 dataset
(Litterfall_Ahmed97).
ac = ((LeafBiog at date i
-LeafBiog at date i-1, dataset = leafbiom97) + (littperiod at date i,
dataset= Litterfall_Ahmed97))/(sum (GPP from date i-1 to date i,
dataset=GPP_Ahmed13)/2) .
#code for loop
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')
for (i in 2:75){
(leafbiom97$LeafBiog[leafbiom97$Date == "i"] -
leafbiom97$LeafBiog[leafbiom97$Date == "i-1"]+
Litterfall_Ahmed97$littperiod[Litterfall_Ahmed97$Date =="i"])/
(sum(GPP_Ahmed13$GPP[GPP_Ahmed13$Date >= "i-1" &
GPP_Ahmed13$Date <= "i"])/2)
}
#Error in charToDate(x) :
Thanks for your help
Ahmed Attia, Ph.D.
Agronomist & Soil Scientist
For loop
3 messages · Ahmed Attia, Rui Barradas, Nordlund, Dan (DSHS/RDA)
Hello, Without correcting your code, it's obvious that the expressions like leafbiom97$Date == "i" and all others with"i" and "i - 1" are wrong. leafbiom97$Date is of class Date, not character. And, besides, for(i in ...) makes of i a numeric variable that has nothing to do with "i". This "i" will remain the character string "i" for all iterations of the loop. Hope this helps, Rui Barradas Em 22-06-2017 17:50, Ahmed Attia escreveu:
leafbiom97$Date == "i"
For a reproducible example, you need to give us some example data, at least dput(head(leafbiom97)) dput(head(Litterfall_Ahmed97)) dput(head(GPP_Ahmed13)) Dan Daniel Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ahmed
Attia
Sent: Thursday, June 22, 2017 9:50 AM
To: r-help
Subject: [R] For loop
Hello R users,
The code below is for loop in R that I want to do to following
calculation at each time i and i-1 in 2:75 dataset
(Litterfall_Ahmed97).
ac = ((LeafBiog at date i
-LeafBiog at date i-1, dataset = leafbiom97) + (littperiod at date i,
dataset= Litterfall_Ahmed97))/(sum (GPP from date i-1 to date i,
dataset=GPP_Ahmed13)/2) .
#code for loop
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')
for (i in 2:75){
(leafbiom97$LeafBiog[leafbiom97$Date == "i"] -
leafbiom97$LeafBiog[leafbiom97$Date == "i-1"]+
Litterfall_Ahmed97$littperiod[Litterfall_Ahmed97$Date =="i"])/
(sum(GPP_Ahmed13$GPP[GPP_Ahmed13$Date >= "i-1" &
GPP_Ahmed13$Date <= "i"])/2)
}
#Error in charToDate(x) :
Thanks for 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.