我想得到最后一个列表元素的列均值,这是一个稀疏矩阵乘以一个常规矩阵。然而,每当我使用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
知道造成这个错误的原因是什么,或者我能做些什么呢?
答案 0 :(得分:1)
看起来显式调用Matrix包中的colMeans函数有效:
> Matrix::colMeans(tail(out, 1)[[1]])
# [1] 4.48 5.48
感谢user20650提供此建议。