[R-sig-eco] doubt in generating loops with For

Rodrigues rodriguesmsb at gmail.com
Fri Apr 17 13:35:32 CEST 2015


Hello Thiago,


You keep doing the same mistake. When you pass z as subset to comm z will be
the z th column and you only has 10 columns so you will always receive the
out of bound. If I understood correctly you are trying to repeat a process
1000 times if yes follow a code above. I'm not sure that the nested loop is
the right thing but it works.

I don't know the diversity index that you are computing so I'm sorry if it's
not the result that you are looking for. But anyways the code now works so I
believe that you can fix it if i did wrong.

regards


library(vegan)


comm<-t(data.frame(sp1=rpois(10, 1), sp2=rpois(10,1), sp3=rpois(10,1),
                   sp4=rpois(10,1), sp5=rpois(10,1)))

traits<-t(data.frame(sp1=rnorm(5,1.2,.2),sp2=rnorm(5,2,.5),
                     sp3=rnorm(5,3,.3), sp4=rnorm(5,0.5,.2),
                     sp5=rnorm(5,.9,.6)))

dist<-vegdist(traits,"gower")
comm<-as.matrix(comm)

results<-list()

for (i in 1:1000){
    for (z in ncol(comm)){
        comm1<-apply(comm,2,sample)
         results[i]<-(((t(comm1[, z]) %*% (as.matrix(dist)^2) %*%
comm1[,z])/2/(sum(comm1[, z])^2))[1,1])
    }
}




--
View this message in context: http://r-sig-ecology.471788.n2.nabble.com/doubt-in-generating-loops-with-For-tp7579393p7579401.html
Sent from the r-sig-ecology mailing list archive at Nabble.com.



More information about the R-sig-ecology mailing list