[英]simultaneous matrix operations in all list objects in R
I have a list of matrices in R as follows,我在 R 中有一个矩阵列表,如下所示,
set.seed(1234)
data <- matrix(rnorm(3*4,mean=0,sd=1), 3, 4)
results <- lapply(1:ncol(data), function(i) outer(data[, i], data[, i]))
which results in:结果是:
[[1]]
[,1] [,2] [,3]
[1,] 1.4570077 -0.33487534 -1.3089918
[2,] -0.3348753 0.07696698 0.3008557
[3,] -1.3089918 0.30085569 1.1760127
[[2]]
[,1] [,2] [,3]
[1,] 5.502298 -1.0065968 -1.1870541
[2,] -1.006597 0.1841480 0.2171611
[3,] -1.187054 0.2171611 0.2560926
[[3]]
[,1] [,2] [,3]
[1,] 0.3303260 0.3141712 0.3244131
[2,] 0.3141712 0.2988064 0.3085474
[3,] 0.3244131 0.3085474 0.3186061
[[4]]
[,1] [,2] [,3]
[1,] 0.7921673 0.4247196 0.8886017
[2,] 0.4247196 0.2277129 0.4764227
[3,] 0.8886017 0.4764227 0.9967755
I want for each list object to sum the columns and find the minimum of these summation.我希望每个列表 object 对列求和并找到这些求和的最小值。 For example
min.results[[1]] = min(-0.186,0.042,0.167)=-0.186
.例如
min.results[[1]] = min(-0.186,0.042,0.167)=-0.186
。
We may use sapply
to loop over the list
, get the column wise sum ( colSums
) and return with the min
imum我们可以使用
sapply
循环遍历list
,获取按列求和 ( colSums
) 并返回min
sapply(results, \(x) min(colSums(x)))
-output -输出
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
Or using collapse
或者使用
collapse
library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.