5.11 Updating variables within a for loop
We also need to update the allele frequency in every iteration of the for loop. We do this by adding a freq
variable that keeps track of the current AF:
# start an initial AF of 0.5
<- 0.5
freq
for (i in 1:3) {
# run rbinom to generate the AF for the next generation
<- rbinom(n = 1, size = 100, prob = freq) / 100
new_freq # print new AF
print(new_freq)
# update `freq` in each iteration of the loop
<- new_freq
freq }
## [1] 0.53
## [1] 0.51
## [1] 0.52