我是R的新用户,我试图编写一个用于模拟物种入侵和社区稳定性的脚本.我几乎完成了它,我在循环中只有一个小问题.
我有40种(1,2,......)的池,我通过连续入侵创建了一个社区.社区中的物种会离开入侵者池,除非它们灭绝(我设置了密度阈值).
我想要很多入侵(> 4000)所以我创建了一个4000到1到40之间的数字(random.order),但我有一个问题,因为我的物种密度矩阵(init.x)的数量不同元素作为我的向量.
time<- list(start=0,end=4000,steps=100)
# Initial conditions (set all species to zero in the beginning)
init.x <- runif(n)*0
# generate random order in which species are introduced
init.order<- sample(1:n)
order<-rep(order,100)
random.order<-sample(order,size=length(order))
outt <- init.x
**for (i in 1:4000){
# Introduce 1 new species (according to vector "random.order") with freq 1000*tol
# if the species is not yet in the init.x matrix
if (init.x[random.order[i]]<tol) {init.x[random.order[i]] <- 1000*tol}**
# integrate lvm model
out <-n.integrate(time=time,init.x=init.x,model=lvm)
# save out and attach it …Run Code Online (Sandbox Code Playgroud)