[R] Combining information from two matrices

arun smartpink111 at yahoo.com
Thu Sep 12 04:10:47 CEST 2013


Hi,

May be this helps:
mat1<- matrix(c(1,1,0,0,0,1,0,1,0),ncol=3,dimnames=list(paste("Plant",1:3), paste("Pollinator",1:3)),byrow=TRUE)
 mat2<- matrix(c(1,1,0,1,0,1,0,1,0),ncol=3,dimnames=list(paste("Plant",c(1,4,5)), paste("Pollinator",c(1,2,4))),byrow=TRUE)
dat1<- as.data.frame(mat1)
 dat2<- as.data.frame(mat2)
toadddat1<-setdiff(colnames(dat2),colnames(dat1))
toadddat2<-setdiff(colnames(dat1),colnames(dat2))
dat2[,evalq(toadddat2)]<-0
res<- as.matrix(dat2[!row.names(dat1)%in% row.names(dat2),!colnames(dat2)%in%evalq(toadddat1)] )
res
#        Pollinator 1 Pollinator 2 Pollinator 3
#Plant 4            1            0            0
#Plant 5            0            1            0
A.K.





Hello all 
I have been trying to find a solution for this for a while, and I hope you can help me. 
I have two matrices. Both are interaction matrices between plants 
and pollinators, and have plants as rows and pollinators as columns. The
 matrix is a presence/absence matrix with 1´s for interaction and 0´s 
for no interaction. Here are simplified examples: 

Matrix 1: 
             Pollinator 1     Pollinator 2     Pollinator 3 
Plant 1          1                   1                  0 
Plant 2          0                   0                  1 
Plant 3          0                   1                  0 

Matrix 2: 
             Pollinator 1     Pollinator 2     Pollinator 4 
Plant 1          1                   1                  0 
Plant 4          1                   0                  1 
Plant 5          0                   1                  0 

What I need is to make a new matrix with the columns 
(pollinators) from matrix 1 and the rows (plants) and entries from 
matrix 2, but only with those rows that are not in matrix 1. Like this: 

            Pollinator 1     Pollinator 2     Pollinator 3 
Plant 4          1                   0                  0 
Plant 5          0                   1                  0 

Hope you can help. Thanks a lot in advance. 
Daniel



More information about the R-help mailing list