我有几千个距离矩阵(从使用as.dist()的矩阵转换而来),并且希望计算每个矩阵元素的平均值、sd、中值等。
为了说明:
Matrix.A
1
7 1
5 2 1Matrix.B
2
3 4
1 1 3等等。
例如,如果我想得到我可以做的各个元素的总和:
Sum.Matrix <- Matrix.A + Matrix.BSum.Matrix
3
10 5
6 3 4但如果我有数千个这些矩阵呢?我如何不仅计算每个元素的和,而且计算平均值、sd等?所有矩阵都存储在一个列表中。
发布于 2015-05-31 18:15:24
试一试
lst2 <- lapply(lst1, as.matrix)
dim1 <- sapply(lst2, dim)[,1]
l <- length(lst1)
ar1 <- array(unlist(lst2), dim=c(dim1, l))
as.dist(apply(ar1, 1:2, sum))
as.dist(apply(ar1, 1:2, mean))
as.dist(apply(ar1, 1:2, sd))数据
set.seed(24)
lst1 <- lapply(1:4, function(i) dist(sample(1:10,4, replace=TRUE)))https://stackoverflow.com/questions/30559159
复制相似问题