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

Petr Pikal
Tue, 16 Nov 2004 08:11:33 -0000

Hi

>From what you write it seems to me a work for outer(). Did you
try it?

Cheers
Petr


On 15 Nov 2004 at 22:26, Timothy W. Victor wrote:

> R-List,
>
> 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 like:
>
> 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.
>
> Best,
>
> Tim
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz