1

我想显示一个没有小的 [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) 

但没有一个按预期工作。

4

1 回答 1

2

所以你只是缺少标题?像这样的东西怎么样

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) 
于 2018-01-16T21:15:11.363 回答