Thiago, It's all about indexing:
rn <- union(rownames(matrix1), rownames(matrix2)) cn <- union(colnames(matrix1), colnames(matrix2)) x <- array(0, dim=c(length(rn), length(cn)), dimnames=list(rn, cn)) x[rownames(matrix1), colnames(matrix1)] <- matrix1 x[rownames(matrix2), colnames(matrix2)] <- matrix2 x Cheers, Peter -- Péter Sólymos, Dept Biol Sci, Univ Alberta, T6G 2E9, Canada AB soly...@ualberta.ca, Ph 780.492.8534, http://psolymos.github.com Alberta Biodiversity Monitoring Institute, http://www.abmi.ca Boreal Avian Modelling Project, http://www.borealbirds.ca On Sun, May 13, 2012 at 8:50 PM, Thiago Gonçalves-Souza <tgoncalves.so...@gmail.com> wrote: > Dear all, > > I have two species-by-site matrices and I'd like to combine them in one > matrix. The rows (n=20) of each matrix present plants 1-20. So, the new > matrix should have 40 rows. As these matrices have some shared species and > some exclusive species, the new matrix should have the species from the two > matrix, but when certain species that doesn't occur for example in Matrix > 1, its value will be 0. I've tried the functions merge, cbind and rbind but > I wasn't able to do it. Please, see the following example to make it clear > ;-) > > > By using for example the following matrices: > > matrix1 <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE, > dimnames = list(c("plant1", "plant2"), > c("sp1", "sp2", "sp3"))) > > matrix2 <- matrix(c(1,2,3, 0,21,9), nrow = 2, ncol=3, byrow=TRUE, > dimnames = list(c("plant3", "plant4"), > c("sp1", "sp2", "sp4"))) > > I'd like to combine them like this: > > new.matrix <- matrix(c(1, 2, 3, 0, 11, 12, 13, 0, 1, 2, 0, 3, 0,21,0,9), > nrow = 4, ncol=4, byrow=TRUE, > dimnames = list(c("plant1", "plant2", "plant3", "plant4"), > c("sp1", "sp2", "sp3", "sp4"))) > > Once I have 200 columns and 80 rows, I'm pretty sure that R can help me to > do it automatically ;-) > > > All the best, > Thiago. > > > -- > Thiago Gonçalves-Souza, Ms. > Universidade Estadual Paulista (UNESP) > Departamento de Zoologia e Botânica > Programa de Pós-Graduação em Biologia Animal (Doutorado) > E-mail alternativo: thiagoara...@gmail.com > Home page:<http://www.wix.com/thiagocalvesouza/tg-s> > > [[alternative HTML version deleted]] > > > _______________________________________________ > R-sig-ecology mailing list > R-sig-ecology@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology > _______________________________________________ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology