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

时间:2018-01-16 21:06:28

标签: r matrix lapply names write.table

我想显示一个矩阵列表(不是单个矩阵,就像其他地方一样),没有小的[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) 

但没有一个按预期工作。

1 个答案:

答案 0 :(得分: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)