稀疏矩阵的 colMeans 将存储为列表最后一个元素的矩阵乘以



我想获取最后一个列表元素的列均值,这是一个稀疏矩阵乘以常规矩阵。 但是,每当我使用colMeans时,我都会收到错误。 例如:

# Use the igraph package to create a sparse matrix
library(igraph)
my.lattice <- get.adjacency(graph.lattice(length = 5, dim = 2))
# Create a conformable matrix of TRUE and FALSE values
start <- matrix(sample(c(TRUE, FALSE), 50, replace = T), ncol = 2)
# Multiply the matrix times the vector, and save the results to a list
out <- list()
out[[1]] <- my.lattice %*% start
out[[2]] <- my.lattice %*% out[[1]]
# Try to get column means of the last element
colMeans(tail(out, 1)[[1]])  # Selecting first element because tail creates a list
# Error in colMeans(tail(out, 1)[[1]]) : 
#  'x' must be an array of at least two dimensions
# But tail(out, 1)[[1]] seems to have two dimensions
dim(tail(out, 1)[[1]])
# [1] 25  2

知道导致此错误的原因,或者我可以做些什么吗?

看起来从矩阵包中显式调用 colMeans 函数是有效的:

> Matrix::colMeans(tail(out, 1)[[1]])
# [1] 4.48 5.48

感谢用户20650的这个建议。

相关内容

  • 没有找到相关文章

最新更新