Your "solution" is unnecessarily complicated. There is no need to
convert
to data frames and no need to eval labels -- simple indexing by column and
row names will do. I think the following code is pretty self-explanatory:
(Starting with the same mat1 and mat2)
rnames <- setdiff(rownames(mat2),rownames(mat1))
mat3 <- matrix(0,nr=length(rnames),nc=ncol(mat1)) ## correct size, all
0's
dimnames(mat3)<- list(rnames,colnames(mat1)) ## row and column names as
requested
wh <- intersect(colnames(mat1),colnames(mat2))
mat3[,wh] <- mat2[rnames,wh]
> mat3
Pollinator 1 Pollinator 2 Pollinator 3
Plant 4 1 0 0
Plant 5 0 1 0
Cheers,
Bert
Pollinator 1 Pollinator 2 Pollinator 3
Plant 4 1 0 0
Plant 5 0 1 0
On Wed, Sep 11, 2013 at 7:10 PM, arun <smartpink111@yahoo.com> wrote:
> 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
>
> ______________________________________________
> 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.
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
[[alternative HTML version deleted]]