Re: [R] intersect() question

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun 16 Jul 2006 - 05:35:33 EST

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@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@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
>



R-help@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 Received on Sun Jul 16 05:39:35 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 16 Jul 2006 - 08:15:08 EST.

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