I have a function to simulate genetic drift and I would like to loop it over multiple values of each parameter The function is below:
wright.fisher<-function(p,Ne,nsim,t){ N <-Ne/2 NA1 <- 2*N*p NA2 <- 2*N*(1-p) k <- matrix(0, nrow = max(2, t), ncol = nsim) k[1,] <- rep(NA1, nsim) for (j in 1:nsim) { for (i in 2:t) { k[i, j] <- rbinom(1, 2*N, prob = k[i-1, j] / (2*N)) } } k <- as.matrix(k/(2*N)) t(k) } I've attempted to loop it over t (generations of drift), but that fails, the following is my code:
locifreq<-runif(49, .4, 0.8) gen <- 2:99 looppop<-list() for (i in 2:length(gen)){ looppop[i]<-lapply(locifreq,wright.fisher,3000,4,gen[i]) } Doing that results in an error for each iteration -
> warnings() Warning messages: 1: In pop[i] <- lapply(locifreq, wright.fisher, 3000, 4, ... : number of items to replace is not a multiple of replacement length 2: In pop[i] <- lapply(locifreq, wright.fisher, 3000, 4, ... : number of items to replace is not a multiple of replacement length I suspect that the issue may be in storing the output into a matrix, and perhaps the function isn't accessing the matrix correctly, but i'm not sure.
Thanks!

looppopbut then populatepopinside theforloop. Can you please edit your question to include a minimal reproducible example that reproduces the warning.