0

我有一个类似于下面的列表,其中包含我想对其进行单独操作的矩阵。

data <- data.frame(matrix(data = c(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,2,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2), nrow = 8, ncol = 4, byrow = TRUE) )
matrix_list <- list(data[1:4, ], data[5:8, ])

我知道我可以分别对每个对象进行矩阵运算,就像这样

eigen(matrix_list[[1]])

我可以用for循环对所有项目进行操作

for (i in 1:2){print(eigen((data_list[[i]])))}

如何跳过for循环并直接对列表进行操作?如果我能做类似“eigen(matrix_list)”之类的事情,那就太好了

4

1 回答 1

3

用于lapply对列表进行操作

lapply(matrix_list, eigen)
[[1]]
[[1]]$values
[1] 1 1 1 1

[[1]]$vectors
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    1
[2,]    0    0    1    0
[3,]    0    1    0    0
[4,]    1    0    0    0


[[2]]
[[2]]$values
[1] 2 2 2 2

[[2]]$vectors
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    1
[2,]    0    0    1    0
[3,]    0    1    0    0
[4,]    1    0    0    0

如果您只对值或向量感兴趣,您可以使用以下方法选择它们:

Eigen <- lapply(matrix_list, eigen)
> sapply(Eigen, '[', 'values') # Extrating eigen values
$values
[1] 1 1 1 1

$values
[1] 2 2 2 2

> sapply(Eigen, '[', 'vectors') # Extrating eigen vectors
$vectors
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    1
[2,]    0    0    1    0
[3,]    0    1    0    0
[4,]    1    0    0    0

$vectors
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    1
[2,]    0    0    1    0
[3,]    0    1    0    0
[4,]    1    0    0    0

> 
于 2012-10-05T12:26:24.927 回答