多种矩阵的打印方法 [英] Print method for multiple matrices
本文介绍了多种矩阵的打印方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以建议在终端窗口中并排print
几个矩阵的方法.
Could someone please suggest a method to print
several matrices side by side in the terminal window.
对于矩阵m1
和m2
,我想要下面的期望输出.
For the matrices m1
and m2
, I would like the desired output below.
m1 <- m2 <- matrix(1:4, nrow=2, dimnames=list(c("a", "b"), c("d", "e")))
所需的输出
m1 m2
d e d e
a 1 3 a 1 3
b 2 4 b 2 4
原因是我在计算中使用了多个2x2矩阵,并希望在Rmarkdown文档中显示.打印长度方式时,它会占用太多页面.谢谢.
The reason is that I have several 2x2 matrices that i am using in calculations and want to show in a Rmarkdown doc. It takes up a bit too much of the page when printing length ways. Thanks.
编辑
我试图解决的问题
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 d e m2 f g
#1 a 1 3 v 1 3
#2 b 2 4 w 2 4
但这当然看起来不太好.
But this of course doesnt look very good.
推荐答案
有点破绽,但我相信这就是您想要的:
A little hack-ish, but I believe it 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
d e d e d e d e
a 1 3 a 1 3 a 1 3 a 1 3
b 2 4 b 2 4 b 2 4 b 2 4
这篇关于多种矩阵的打印方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文