R中所有列表对象的同时矩阵运算
simultaneous matrix operations in all list objects in R
我在 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]))
这导致:
[[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
我希望每个列表对象对列求和并找到这些求和的最小值。例如 min.results[[1]] = min(-0.186,0.042,0.167)=-0.186
.
我们可以使用 sapply
遍历 list
,得到列式总和 (colSums
) 和 return 以及 min
imum
sapply(results, \(x) min(colSums(x)))
-输出
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
或使用collapse
library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
我在 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]))
这导致:
[[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
我希望每个列表对象对列求和并找到这些求和的最小值。例如 min.results[[1]] = min(-0.186,0.042,0.167)=-0.186
.
我们可以使用 sapply
遍历 list
,得到列式总和 (colSums
) 和 return 以及 min
imum
sapply(results, \(x) min(colSums(x)))
-输出
[1] -0.1868594 -0.7138005 0.9215250 1.1288552
或使用collapse
library(collapse)
fmin(dapply(results, colSums))
[1] -0.1868594 -0.7138005 0.9215250 1.1288552