Printing Method for Multiple Matrices

Can someone suggest a method to print multiple matrices side by side in a terminal window.

For the matrices m1 and m2 I would like to get the desired result below.

 m1 <- m2 <- matrix(1:4, nrow=2, dimnames=list(c("a", "b"), c("d", "e"))) 

Required conclusion

 m1 m2 dede a 1 3 a 1 3 b 2 4 b 2 4 

The reason is because I have several 2x2 matrices that I use in my calculations and want to show in the Rmarkdown document. This takes up too much of the page when printing length paths. Thank you

EDIT

My attempt to solve

 fn <- function(x) setNames(data.frame(.=paste(" ", rownames(x)), x, check.names=F, row.names=NULL),c(paste(substitute(x)), colnames(x))) cbind(fn(m1), fn(m2)) # m1 de m2 fg #1 a 1 3 v 1 3 #2 b 2 4 w 2 4 

But this, of course, is not very good.

+4
source share
1 answer

Little hack ish, but I believe that this is what you want:

 m1 <- m2 <- m3 <- m4 <- matrix(1:4, nrow=2, dimnames=list(c("a", "b"), c("d", "e"))) fn <- function(x) setNames(data.frame(.=paste("", rownames(x)), x, check.names=F, row.names=NULL),c(" ", colnames(x))) matrix.names <- Filter( function(x) 'matrix' %in% class( get(x) ), ls(pattern = "m") ) matrix.list <- lapply(matrix.names, get) matrix.chain <- do.call(cbind, lapply(matrix.list, fn)) cat(" ", paste0(matrix.names, collapse = " "), "\n"); print(matrix.chain, row.names = FALSE) m1 m2 m3 m4 dededede a 1 3 a 1 3 a 1 3 a 1 3 b 2 4 b 2 4 b 2 4 b 2 4 
+2
source

All Articles