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
freq <- 0.5
for (i in 1:3) {
# run rbinom to generate the AF for the next generation
new_freq <- rbinom(n = 1, size = 100, prob = freq) / 100
# print new AF
print(new_freq)
# update `freq` in each iteration of the loop
freq <- new_freq
}
## [1] 0.53
## [1] 0.51
## [1] 0.52