# [R] intersect() question

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jul 15 21:35:33 CEST 2006

```Define a generalized crossproduct and then apply it with
the indicated function.  Multiply the diagonal elements by
zero as the sample output seems to be forcing them that way.

mm <- matrix(c(5, 1, 6, 6, 5, 7, 7, 3, 2), 3) # test matrix

# generalized crossproduct
inner <- function(a,b=a,f=crossprod)
apply(b,2,function(x)apply(a,2,function(y)f(x,y)))

inner(mm, f = function(x,y) length(intersect(x,y))) * !diag(ncol(mm))

On 7/15/06, Andrej Kastrin <andrej.kastrin at siol.net> wrote:
> Hi,
>
> I have a matrix containing ID numbers in each column. I would like to
> program function which calculate common number of ID numbers between
> each pair of columns.
>
> Suppose:
>
> 5 6 7
> 1 5 3
> 6 7 2
>
> Then the result should be:
>
> 0 2 0
> 2 0 1
> 0 1 0
>
> The main problem is how to implement intersect() function to walk
> through each pair of columns and write result to result matrix.
>
> Thanks in advance for any suggestion, Andrej
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help