如何使用 Prcomp 在 R 中提取 PCA 的摘要作为数据框?

How do I extract summary of PCA as a dataframe in R using Prcomp?

res.pca = prcomp(y, scale = TRUE)
summ=summary(res.pca)
summ

给我输出 Desired Output

我想将此摘要更改为数据框,

我试过使用 do.call(cbind, lapply(res.pca, summary)) 但它给了我 Min/Max 的总结,而不是我想要的。

请注意,我不想从列名中提取值,我寻求可以使用的通用解决方案。

您要查找的内容在summary(res.pca)的"element"importance中:

示例取自 Principal Components Analysis - how to get the contribution (%) of each parameter to a Prin.Comp.?

a <- rnorm(10, 50, 20)
b <- seq(10, 100, 10)
c <- seq(88, 10, -8)
d <- rep(seq(3, 16, 3), 2)
e <- rnorm(10, 61, 27)

my_table <- data.frame(a, b, c, d, e)
res.pca <- prcomp(my_table, scale = TRUE)

summary(res.pca)$importance 
#                          PC1    PC2    PC3     PC4       PC5
#Standard deviation     1.7882 0.9038 0.8417 0.52622 9.037e-17
#Proportion of Variance 0.6395 0.1634 0.1417 0.05538 0.000e+00
#Cumulative Proportion  0.6395 0.8029 0.9446 1.00000 1.000e+00

class(summary(res.pca)$importance)
#[1] "matrix"

N.B.:
当你想 "study" 一个对象时,在它上面使用 str 会很方便。在这里,您可以 str(summary(pca) 来查看信息的位置,从而了解您可以从哪里获得想要的信息:

str(summary(res.pca))

List of 6
 $ sdev      : num [1:5] 1.79 9.04e-01 8.42e-01 5.26e-01 9.04e-17
 $ rotation  : num [1:5, 1:5] 0.278 0.512 -0.512 0.414 -0.476 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "a" "b" "c" "d" ...
  .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
 $ center    : Named num [1:5] 34.9 55 52 9 77.8
  ..- attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
 $ scale     : Named num [1:5] 22.4 30.28 24.22 4.47 26.11
  ..- attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
 $ x         : num [1:10, 1:5] -2.962 -1.403 -1.653 -0.537 1.186 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
 $ importance: num [1:3, 1:5] 1.788 0.64 0.64 0.904 0.163 ...
 ..- attr(*, "dimnames")=List of 2
 .. ..$ : chr [1:3] "Standard deviation" "Proportion of Variance" "Cumulative Proportion"
 .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
- attr(*, "class")= chr "summary.prcomp"