[R] Pairwise Distances -- How to vectorize the loop

Timothy W. Victor tvictor at dolphin.upenn.edu
Tue Nov 16 04:26:30 CET 2004


I'm trying to compute pairwise distances among pairs of observations, 
which each pair containing data from 2 groups. There are more than 
100000 unique pairs. I have programmed a distance function that has 
three parameters, a vector of covariates from the ith observation in 
Group 1, a vector of covarites from the jth observation in Group 2, and 
a weighting matrix.

I have used expand.grid to create a matrix containing all possible pairs 
of Keys (unique identifiers) from Group 1 and Group 2 and a placeholder 
for each pairwise distance in an effort to pre-allocate memory.

The data containing the covariates are currently in a matrix that looks 

x1 x2 x3 ... xn Key Group

where x1,...,xn are the covariates of interest. The Key values 
correspond to the row number.

I'm trying to figure out a way to calculate all of the distances by 
vectorizing a loop. I'm trying to avoid the nested-loop paradigm. I'm 
sure I'm missing something quite obvious.

Any insights would be appreciated.



More information about the R-help mailing list