I have a list of matrices in R as follows,
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. For example min.results[[1]] = min(-0.186,0.042,0.167)=-0.186
.
CodePudding user response:
We may use sapply
to loop over the list
, get the column wise sum (colSums
) and return with the min
imum
sapply(results, \(x) min(colSums(x)))
-output
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
Or using collapse
library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005 0.9215250 1.1288552