如何在prcomp中访问主要组件rownames?

时间:2014-05-28 08:57:27

标签: r

我可能不会在这里解释得很好。如何在使用后访问prcomp中的名称列,如下所示?我想将此作为后续图表的列表。

prcomp(USArrests)
Standard deviations:
[1] 83.732400 14.212402  6.489426  2.482790

Rotation:
            PC1         PC2         PC3         PC4
Murder   0.04170432 -0.04482166  0.07989066 -0.99492173
Assault  0.99522128 -0.05876003 -0.06756974  0.03893830
UrbanPop 0.04633575  0.97685748 -0.20054629 -0.05816914
Rape     0.07515550  0.20071807  0.97408059  0.07232502

我想访问提取列表"谋杀,攻击,UrbanPop,强奸"。

1 个答案:

答案 0 :(得分:0)

使用str

总是有帮助的
res <- prcomp(USArrests)
str(res)
# List of 5
# $ sdev    : num [1:4] 83.73 14.21 6.49 2.48
# $ rotation: num [1:4, 1:4] 0.0417 0.9952 0.0463 0.0752 -0.0448 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
# .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4"
# $ center  : Named num [1:4] 7.79 170.76 65.54 21.23
# ..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
# $ scale   : logi FALSE
# $ x       : num [1:50, 1:4] 64.8 92.8 124.1 18.3 107.4 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
# .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4"
# - attr(*, "class")= chr "prcomp"

然后我们可以这样做:

rownames(res$rotation)
#[1] "Murder"   "Assault"  "UrbanPop" "Rape"