Thank you very much. This was very helpful. Exactly what I wanted.
thanks
Hermann


2013/5/16 arun <smartpink111@yahoo.com>

> Hi,
> May be this helps:
>
> c1<-combn(seq_len(ncol(m)),2)
>  mat1<- matrix(0,ncol=3,nrow=3,dimnames=list(colnames(m),colnames(m)))
> vec1<-unlist(lapply(seq_len(ncol(c1)),function(i) {m1<-m[,c1[,i]];
> length(which(m1[,1]!=0 & m1[,2]!=0)) }))
> mat1[lower.tri(mat1)]<-vec1
>  mat1
> #    rs1 rs2 rs3
> #rs1   0   0   0
> #rs2   1   0   0
> #rs3   2   1   0
>
> #Converting to sparseMatrix
> library(Matrix)
>  mat2<-as(mat1,"sparseMatrix")
> #or
>
> mat2<-Matrix(mat1,sparse=TRUE)
> 3 x 3 sparse Matrix of class "dtCMatrix"
> #    rs1 rs2 rs3
> #rs1   .   .   .
> #rs2   1   .   .
> #rs3   2   1   .
>
>
>
>
> A.K.
>
>
>
> ----- Original Message -----
> From: Hermann Norpois <hnorpois@gmail.com>
> To: r-help <r-help@r-project.org>
> Cc:
> Sent: Wednesday, May 15, 2013 3:57 PM
> Subject: [R] matrix - pairwise comparison of columns
>
> Hello,
>
> I would like to do something with a matrix:
> 1) The columns should be compared pairwise.
> 2) And the result should be a matrix.
>
> I try to illustrate the problem with a testset.
>
> > m
>      rs1 rs2 rs3
> [1,]   1   1   1
> [2,]   0   1   0
> [3,]   2   0   1
> > dput (m)
> structure(c(1, 0, 2, 1, 1, 0, 1, 0, 1), .Dim = c(3L, 3L), .Dimnames = list(
>     NULL, c("rs1", "rs2", "rs3")))
> > which (m[,1] !=0&m[,2] != 0) #How many rows between two columns that are
> both non zero
>
> How can I automate the pairwise which-statement?
>
> As result I would like to have a matrix like this
>
>      rs1 rs2 rs3
> rs1
> rs2  1
> rs3  2    1
>
> Is there a tool in R that helps?
>
> Thanks
> Hermann
>
>     [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
>

	[[alternative HTML version deleted]]

