iteration using a loop in R

#1
Hi,

I have two vectors as follows:

Code:
popd = vector(mode='numeric', 100)
popr = vector(mode='numeric', 100)
These represent the initial distribution within 2 populations.
I want to define these e.g.

Code:
popd[]=0.01
popr[]=0.01
These populations will then transform according to the following for loop:

Code:
loop <- for(i in 1:100)
{π <- function(S)
	{x <- sum(popd[1:S])
	return((100-S)*x)}
µ <- function(A)
	{share <- vector(mode='numeric', (101-A))
	share[] = A:100
	return(share%*%popr[A:100])}
for(S in 1:100){vectorπ <- vector(mode='numeric', 100)
	vectorπ[S]=π(S)}
for(A in 1:100){vectorµ <- vector(mode='numeric', 100)
	vectorµ[A]=µ(A)}
av.payoffd <- sum(popd * vectorπ)
av.payoffr <- sum(popr * vectorµ)
newd <- vector(mode='numeric', 100)
	for(S in 1:100){newd[S]=(popd[S]/(av.payoffd))}
newr <- vector(mode='numeric', 100)
	for(A in 1:100){newr[A]=(popr[A]/(av.payoffr))}
newpopd <- vectorπ * newd
	test1[i,2]=(sum(newpopd * (1:100)))
newpopr <- vectorµ * newr
	test1[i,4]=(sum(newpopr * (1:100)))
mutationmatrix <- function(e)
	{mut <- c(e, 1-(2*e), e)
	return(matrix(c(1-e, e, (rep(c(rep098, mut), times=98)), rep098, e, 1-e), nrow=100))}
mutpopd <- function(e)
	{mutationmatrix(e) %*% newpopd}
	test1[i,3]=(sum(mutpopd(e) * (1:100)))
mutpopr <- function(e)
	{mutationmatrix(e) %*% newpopr}
	test1[i,5]=(sum(mutpopr(e) * (1:100)))
if(i<100){
	popd=mutpopd(e)
	popr=mutpopr(e)
}
if(i==100){
	for(i in 1:100){
	popdistcomp1[i,3]=mutpopd(e)[i,1]
	popdistcomp1[i,6]=mutpopr(e)[i,1]}
}
}
test1 and popdistcomp1 are both matrices I have already defined.
However, when I ask for the results I get:

Code:
test1
         i meanoffer meanmutoffer meanminaccept meanmutminaccept
  [1,]   1       NaN          NaN           100            99.95
  [2,]   2       NaN          NaN           100            99.95
  [3,]   3       NaN          NaN           100            99.95
  [4,]   4       NaN          NaN           100            99.95
  [5,]   5       NaN          NaN           100            99.95
  [6,]   6       NaN          NaN           100            99.95
  [7,]   7       NaN          NaN           100            99.95
  [8,]   8       NaN          NaN           100            99.95
  [9,]   9       NaN          NaN           100            99.95
 [10,]  10       NaN          NaN           100            99.95
and

Code:
popdistcomp1
       p(i)   initialpopd mutatedpopd q(a)   initialpopr mutatedpopr
  [1,] "p1"   "0.01"      "NaN"       "q1"   "0.01"      "0"        
  [2,] "p2"   "0.01"      "NaN"       "q2"   "0.01"      "0"        
  [3,] "p3"   "0.01"      "NaN"       "q3"   "0.01"      "0"        
  [4,] "p4"   "0.01"      "NaN"       "q4"   "0.01"      "0"        
  [5,] "p5"   "0.01"      "NaN"       "q5"   "0.01"      "0"        
  [6,] "p6"   "0.01"      "NaN"       "q6"   "0.01"      "0"        
  [7,] "p7"   "0.01"      "NaN"       "q7"   "0.01"      "0"        
  [8,] "p8"   "0.01"      "NaN"       "q8"   "0.01"      "0"        
  [9,] "p9"   "0.01"      "NaN"       "q9"   "0.01"      "0"        
 [10,] "p10"  "0.01"      "NaN"       "q10"  "0.01"      "0"        
 [11,] "p11"  "0.01"      "NaN"       "q11"  "0.01"      "0"        
 [12,] "p12"  "0.01"      "NaN"       "q12"  "0.01"      "0"        
 [13,] "p13"  "0.01"      "NaN"       "q13"  "0.01"      "0"        
 [14,] "p14"  "0.01"      "NaN"       "q14"  "0.01"      "0"        
 [15,] "p15"  "0.01"      "NaN"       "q15"  "0.01"      "0"        
 [16,] "p16"  "0.01"      "NaN"       "q16"  "0.01"      "0"        
 [17,] "p17"  "0.01"      "NaN"       "q17"  "0.01"      "0"        
 [18,] "p18"  "0.01"      "NaN"       "q18"  "0.01"      "0"        
 [19,] "p19"  "0.01"      "NaN"       "q19"  "0.01"      "0"        
 [20,] "p20"  "0.01"      "NaN"       "q20"  "0.01"      "0"
I've only showed the first 20 entries for the sake of space but they continue in much the same vein, though I should probably say that the last two entries of popdistcomp1 under mutatedpopr are 0.05 and 0.95.

What I'm unsure of is where the "NaN" entries are coming from. I guess there's an issue in the code somewhere, or maybe it's a problem with the structure of my loop. I am a complete beginner, been using R for about 5 days so apologies if I'm being daft.

Thanks for any help,

Lucy
 
Last edited:

TheEcologist

Global Moderator
#2
I have been trying this using a for loop, for(i in 100) and then code, but it's only doing one round. I think it's because I'm not specifying correctly that I want the output (the mutpopd and mutpopr vectors) to be the new input - because I just don't know how! Hopefully someone can help me...if you need more information just let me know.

Hi Lucy,

Your problem may lay in the way you are defining your for loop; for(i in 100).

Here is what happens inside;
Code:
for(i in 100){print(i)}
You see that the loop only prints one value, 100. This is because you have told the loop to interate i into 100, you have not specified any other values i should become!

You need to give the R interpreter (the actual program that evalutes your code) a number of things to change i into... for instance look at what happens when I do this;

Code:
for(i in c('R','programs','are','fun'))
{
print(i)
}
Here R prints out each word I put into the vector, in the order I put it. So it's changing i into each of the words!
What you want is;

Code:
for(i in seq_len(100))
{
#your code here
print(i)
}
or even simpler

Code:
for(i in 1:100)
{
#your code here
print(i)
}
Hope this helps!
 
Last edited:
#3
Thanks for pointing this out, it was actually just sloppy typing on my part, I did have "i in 1:100" in my code but typed wrong into here. I have somewhat solved my problem but am still running into issues...I'm going to edit my question to reflect this, so if you fancy something (slightly..) more challenging take a look!