On 16/11/2007 6:42 PM, Andrew Park wrote: > > Hi there, > > I would like to find a more efficient way of permuting the rows and columns > of a symmetrical matrix that represents ecological or actual distances > between objects in space. The permutation is of the type used in a Mantel > test. > > Specifically, the permutation has to accomplish something like this: > > > Original matrix addresses: > > a11 a12 a13 > > a21 a22 a23 > > a31 a32 a33 > > > Example permutation > > a22 a23 a21 > > a32 a33 a31 > > a12 a13 a11 > > that is relative positions of rows and columns are conserved in the > permutation. > > Basically, I have been doing this in a "for" loop by (1) permuting the raw > data vector using "sample", (2) generating a lower triangular distance matrix > from the permuted raw data using the "distance" function from "ecodist', and > (3) calculating a bunch of statistics including the Mantel correlation and > multiple regression statistics, which are then stored in blank matrices that > were declared prior to beginning the loop. The whole procedure needs to > repeat at least 999 times but 1999 times would be better and 9999 times would > be ideal. > > The problem is, R-users will know, is that using "for" loops like this is > slow, and gets slower the further into the loop you get.
I don't think for loops should slow down. What you may be doing is gradually growing a result vector; that does slow down over time. For example, this is slow: result <- c() for (i in 1:100000) result <- c(result, i) but this is very quick: result <- numeric(100000) for (i in 1:100000) result[i] <- i Duncan Murdoch > > However, I am not a sophisticated programmer, and cannot think of a more > efficient way to do this. > > Thanks in advance, > > Andy Park (University of Winnipeg). > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.