[R] Two matrix loop

Stefan Petersson stefan.petersson at inizio.se
Thu Mar 24 13:23:24 CET 2011


Hi,

I'm trying to create a distance matrix. And it works out somewhat ok. However, I suspect that there are 
some efficiency issues with my efforts. Plz have a look at this:

donor <- matrix(c(3,1,2,3,3,1,4,3,5,1,3,2), ncol=4)
receiver <- 
matrix(c(1,4,3,2,4,3,1,5,1,3,2,1,4,5,3,5,1,3,2,4,5,1,2,3,1,4,5,5,1,2,1,3,4,3,2,5,5,1,4,2,5,4,3,2), ncol=4)

The above creates my two matrices. I have three donors, and eleven receivers (rows), with four 
measures (columns) in each matrix.

And now, I want to apply the daisy() function from the cluster library, to calculate distances between my 
three donors, and eleven receivers. The end result should be a 11x3 matrix with distances between the 
units from the two matrices. I can calculate one distance measure (ie donor 1 and receiver 1). Like this:

library(cluster)
daisy(rbind(donor[1,], receiver[1,]), metric = 'gower')

My first attempt was a simple nested for-loop. But that one was discarded after reading up on efficiency 
issues with for-looping. So I turned to 'apply' with this result:

apply(donor, 1, function(b) apply(receiver, 1, function(a) daisy(rbind(b, a), metric = 'gower')))

      [,1] [,2] [,3]
 [1,] 1.00 0.50 0.75
 [2,] 1.00 0.75 0.75
 [3,] 0.75 1.00 1.00
 [4,] 0.50 0.75 0.75
 [5,] 0.75 1.00 0.75
 [6,] 0.75 1.00 0.50
 [7,] 0.75 0.50 0.75
 [8,] 1.00 1.00 1.00
 [9,] 1.00 0.75 1.00
[10,] 0.75 0.50 1.00
[11,] 0.75 1.00 0.25

However, something tells me that there is a simpler (more efficient) way of doing this. I've been reading 
up on the Matrix library, but I'm having trouble understanding the functions...



More information about the R-help mailing list