# Re: [R] Two matrix loop

From: Thomas Levine <thomas.levine_at_gmail.com>
Date: Sat, 26 Mar 2011 19:12:12 -0400

This way uses a three-dimensional array instead of the nested apply. It seems to take the same amount of time, even on larger datasets, but it may give you ideas.

distance=function(x) daisy(x, metric = 'gower')

matrix(apply(persons,2,distance),,nrow(donor))

Tom

On Thu, Mar 24, 2011 at 8:23 AM, Stefan Petersson <stefan.petersson_at_inizio.se> wrote:
>
> 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)
> 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...
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

R-help_at_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. Received on Sat 26 Mar 2011 - 23:15:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Mar 2011 - 23:20:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.