# [R] need help to generate an intersection matrix

Jim Lemon drjimlemon at gmail.com
Fri Feb 3 01:08:37 CET 2017

```Hi Ana,
Here is one way:

pathway1<-LETTERS[1:5]
pathway2<-c("A","C","F")
pathway3<-c("B","D","E")
intersect.mat<-matrix(0,nrow=3,ncol=3)
rownames(intersect.mat)<-paste("pathway",1:3,sep="")
colnames(intersect.mat)<-paste("pathway",1:3,sep="")
for(row in 1:3) {
for(col in 1:3)
intersect.mat[row,col]<-
sum(get(rownames(intersect.mat)[row])%in%
get(colnames(intersect.mat)[col]))
}

Jim

On Fri, Feb 3, 2017 at 6:54 AM, Li, Aiguo (NIH/NCI) [E]
<liai at mail.nih.gov> wrote:
> Dear all,
>
> I am new to r script and run into some difficulty with this simple task.
> Here is my data: I need to find out the number of mutual intersected elements as shown below
> data
> pathway1             A             B             C              D             E
> pathway2             A             C              F
> pathway3             B             D             E
>
> output
>                                 pathway1             pathway2             pathway3
> pathway1             5                                              2                                              3
> pathway2             2                                              3                                              0
> pathway3             3                                              0                                              3
>
> here is my script
> tb = matrix(data =NA, nrow = 3, ncol = 3)
> for(i in 1:3){
>   for(j in 1:3){
>
>   tb[i,j]=length(intersect(data[i,][!is.na(data[i,])], data[j,][!is.na(data[j,])]))
>   }
> }
>
> Ana
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help