简单的循环编码问题
我是 R 的新用户,我尝试编写一个脚本来模拟物种入侵和群落稳定性。我几乎已经完成了,并且循环中只有一个小问题。
我有一个包含 40 个物种的池(1,2,...),我通过连续的入侵创建了一个群落。群落中的物种离开入侵者池,除非它们灭绝(我设置了密度阈值)。
我想要大量的入侵(> 4000),所以我创建了一个包含 1 到 40 之间的 4000 个数字的向量(随机顺序),但我有一个问题,因为我的物种密度矩阵(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 to outt
outt <- rbind(outt,out)
# generate new time window to continue integration
time <- list(start=time$end, end = time$end+time$end-time$start,
steps=100)
}
我知道这可能非常简单,但我找不到一种方法来编写我的循环,使其具有比物种数量(矩阵中的原始数量)更多的入侵。
多谢,
I am a new user of R and I have tried to write a script for similuting species invasion and community stability. I have almost finished it and I have only one tiny problem in a loop.
I have a pool of 40 species (1,2,...) and I create a community by successive invasions. Species in the community leave the invaders pool unless they go extinct(i put a density threshold value).
I want a lot of invasions (>4000) so I created a vector with 4000 number between 1 and 40 (random.order) but I have a problem because my matrix with the species density (init.x) has not the same number of elements as my vector.
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 to outt
outt <- rbind(outt,out)
# generate new time window to continue integration
time <- list(start=time$end, end = time$end+time$end-time$start,
steps=100)
}
I know this is probably very simple but I can't find out a way to write my loop to have more invasions than the number of species (number of raws in my matrix).
Thanks a lot,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想更改
为
...来解决您的主要问题(检查?样本)。我还没有检查其余的代码,但可能还有更多优化的空间。
You probably want to change
Into
...to solve your main problem (check ?sample). I have not checked the rest of you code, but there may be room for more optimization.
我不清楚你的问题是什么,以及它会进入什么
outt
。您可能需要使用list()
来初始化它。至于选择随机入侵者,你可以尝试:
这避免了 if 语句和预先计算的随机试验的需要(如果选择了群落物种,则可能无法产生入侵)。
I'm not clear on what your problem is, and what it going into
outt
. You may want to initalise it withlist()
.As for choosing a random invader you could try:
This avoids the
if
statement and the need for the pre-computed random trials (which may fail to produce an invasion if a community species is selected).