On Tue, 2006-05-30 at 15:09 -0500, Sundar Dorai-Raj wrote: > > Gavin Simpson wrote: > > Dear List, <snip /> > > Will this help: > > rbind.all <- function(...) { > x <- list(...) > cn <- unique(unlist(lapply(x, colnames))) > for(i in seq(along = x)) { > if(any(m <- !cn %in% colnames(x[[i]]))) { > na <- matrix(NA, nrow(x[[i]]), sum(m)) > dimnames(na) <- list(rownames(x[[i]]), cn[m]) > x[[i]] <- cbind(x[[i]], na) > } > } > do.call(rbind, x) > } > > y <- matrix(c(0,1,1,1,0,0,0,4,4), ncol = 3, byrow = TRUE) > rownames(y) <- c("a","b","c") > colnames(y) <- c("1","2","3") > y2 <- y[2:3, 2:3] > rownames(y2) <- c("x","z") > y3 <- matrix(c(0,1,1,1,0,0,0,4,4,5,6,7), ncol = 4, byrow = TRUE) > rownames(y3) <- c("d","e","f") > colnames(y3) <- c("1","2","3","4") > > rbind.all(y, y2, as.data.frame(y3)) > > It does very little error-checking, so be careful how you use it. > > --sundar
Dear Marc and Sundar, Thank you both for your solutions to my merging problem. Both work well on the size of the real data sets this will be used on. In the end I opted for Sundar's rbind.all() as it has the, welcome but unexpected, side-effect of allowing you to merge more than 2 data frames at the same time - which was something I was wanting to implement at a later date. Sundar, I was thrown a little by the need to as.data.frame one of the incoming matrices. If you leave it out you get very different results, I assume because the matrix "method" is being used instead of rbind.data.frame. It took me a little while to track this down as rbind doesn't do method dispatch in the standard UseMethod way. Once again, many thanks, G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% * Note new Address, Telephone & Fax numbers from 6th April 2006 * %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson ECRC & ENSIS [t] +44 (0)20 7679 0522 UCL Department of Geography [f] +44 (0)20 7679 0565 Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street [w] http://www.ucl.ac.uk/~ucfagls/cv/ London, UK. [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html