Hi,

I have two vectors as follows:

These represent the initial distribution within 2 populations.

I want to define these e.g.

These populations will then transform according to the following for loop:

test1 and popdistcomp1 are both matrices I have already defined.

However, when I ask for the results I get:

and

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

I have two vectors as follows:

Code:

```
popd = vector(mode='numeric', 100)
popr = vector(mode='numeric', 100)
```

I want to define these e.g.

Code:

```
popd[]=0.01
popr[]=0.01
```

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]}
}
}
```

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
```

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"
```

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: