[R] Permutation of a distance matrix

Andrew Park anditopark at hotmail.com
Sat Nov 17 00:42:32 CET 2007

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.

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).

More information about the R-help mailing list