[R] Creating a loop that works....

Michael Rennie mrennie at utm.utoronto.ca
Tue Jul 1 04:11:00 CEST 2003


Hi there,

First off, thanks to everyone who has helped me so far.  Sorry to keep 
pestering you all.

I'm including my code here, and I will comment down it where it is that I am 
having problems figuring out how to write this damn thing.


> temper <- scan("temp2.dat", na.strings = ".", list(Day=0, Temp=0))
Read 366 records
> 
> Day <- temper$Day ; Temp<-temper$Temp ; 
> 
> temp<- cbind (Day, Temp)
> #Day = number of days modelled, Temp = daily avg. temp.
> #temp [,2]
> 
> p<- 0.558626306252032
> ACT <- 1.66764519286918
> 
> Vc<-((CTM-temp[,2])/(CTM-CTO))
> Vr<-((RTM-temp[,2])/(RTM-RTO))
> 
> 
> comp<- cbind (Day, Temp, Vc, Vr)
> 
> 
> bio<-NULL
> M<- length(Day) #number of days iterated
> for (i in 1:M)
+ {
+ 
+ weight<- function(Day)
+ {
+ W<-NULL
+ 	if (Day[i]==1) {W[i] <- Wo}
+ else    if (Day[i]>1) {W[i] <- ((bio[i-1,1]*bio[i-1,9])/Ef)
+ 	}
+ 	W
+ }
+ 
+ W<-weight(Day)

The problem, as many of you have already identified, is right here. I hope I 
finally have the syntax right, but even if the "if else" is coded properly, I 
don't think R can find the values in the second condition I list. I need W in 
each step of the iteration to change slightly, based on the mess of 
calculations below (which are using parameters that I have already specified).  
After all the calculations are made, I hope to get values in bio[i,1] and bio
[i,9] corresponding to the iteration that just occured, then return to the top 
of the loop to combine them into the value of W for the next iteration. What I 
think is happening here is that R is looking for values in the condition before 
they are actually there- the way I've written it, they can't be there until I 
get past the conditional step.  Which means I am coding this all wrong.  That 
said, I'm not sure how else to do it;  the value of W in the next iteration is 
dependent on the values of Gr and W in the previous iteration, with the 
exception of the first one (Day=1).  I've tried defining "bio" as 

bio<-matrix(NA, ncol=9, nrow=366)

but that doesn't help either.

Perhaps my rbind at the end of the file is incorrect? I think maybe I'm getting 
mixed up between calculating vectors and values-- should I be specifying [i] 
for everything below where I am now specifying vecotrs?  

+ #W<-Wo
+ 
+ C<- p*CA*(W^CB)*((comp[,3]^Xc)*(exp(Xc*(1-comp[,3]))))*Pc
+ 
+ ASMR<- (ACT*RA*(W^(RB))*((comp[,4]^Xa)*(exp(Xa*(1-comp[,4])))))
+ 
+ SMR<- (ASMR/ACT)
+ 
+ A<- (ASMR-SMR)
+ 
+ F<- (FA*(comp[,2]^FB)*(exp(FG*p))*C)
+ 
+ U<- (UA*(comp[,2]^UB)*(exp(UG*p))*(C-F))
+ 
+ SDA<- (S*(C-F))
+ 
+ Gr<- (C-(ASMR+F+U+SDA))
+ #Day, Temp, Vc, Vr, W, C, ASMR, SMR, A, F, U, SDA, Gr)
+ 
+ bio<- rbind(c(W, C, ASMR, SMR, A, F, U, SDA, Gr))
+ 
+ dimnames (bio) <-list(NULL, c
("W", "C", "ASMR", "SMR", "A", "F", "U", "SDA", "Gr"))
+ 
+ }
Error: length of dimnames[2] not equal to array extent
Execution halted


-- 
Michael Rennie
M.Sc. Candidate
University of Toronto at Mississauga
3359 Mississauga Rd. N.
Mississauga ON  L5L 1C6
Ph: 905-828-5452  Fax: 905-828-3792




More information about the R-help mailing list