显示没有行名和列名的矩阵列表

Display a list of matrices without row and column names

我想显示一个矩阵列表(不是单个矩阵,正如其他地方所询问的那样),没有小的 [1,] 和 [1] 行和列指示符。

例如,给定 myList:

myList <- list(matrix(c(1,2,3,4,5,6), nrow = 2), matrix(c(1,2,3,4,5,6), nrow = 3))
names(myList) <- c("This is the first matrix:", "This is the second matrix:")

我正在寻找一些函数 myFunction() 将输出:

> myFunction(myList)
$`This is the first matrix:`
 1    3    5
 2    4    6

$`This is the second matrix:`
 1    4
 2    5
 3    6

如果能把列表名周围的$...去掉就更好了,这样会显示:

This is the first matrix:
 1    3    5
 2    4    6

This is the second matrix:
 1    4
 2    5
 3    6

看完所有相关问题后,我试过了

 myList %>% lapply(print, row.names = F)
 myList %>% lapply(prmatrix, collab = NULL, rowlab = NULL)
 myList %>% lapply(write.table, sep = " ",  row.names = F, col.names = F) 

但是 none 按预期工作。

所以你只是错过了 headers?怎么样

library(purrr) #for walk2()
print_with_name <- function(mat, name) {
  cat(name,"\n")
  write.table(mat, sep = " ",  row.names = F, col.names = F)
}
myList %>% walk2(., names(.), print_with_name)