简体   繁体   English

将“负载”对象转换为数据框(R)

[英]Convert a “loadings” object to a dataframe (R)

I am trying to convert an object of type "loadings" to a dataframe in R. However, my attempts to coerce it via as_tibble() or as.data.frame() have not worked. 我正在尝试将类型为“ loadings”的对象转换为R中的数据框。但是,我尝试通过as_tibble()或as.data.frame()强制它。 Here is the code: 这是代码:

iris_pca <- prcomp(iris[1:4], center = TRUE, scale. = TRUE)
iris_pca$rotation[,1:2] %>% 
  varimax() %>% 
  .$loadings

This prints out: 打印输出:

Loadings:
             PC1    PC2   
Sepal.Length  0.596 -0.243
Sepal.Width         -0.961
Petal.Length  0.570  0.114
Petal.Width   0.565       

                PC1  PC2
SS loadings    1.00 1.00
Proportion Var 0.25 0.25
Cumulative Var 0.25 0.50

How can I get this data into a dataframe? 如何将这些数据放入数据框?

From the "loadings" object extract the values as numeric. "loadings"对象中,将值提取为数字。 Coerce them into a matrix. 将它们胁迫成矩阵。 Needed dimensions and names you will find within str(l) . 您可以在str(l)找到所需的尺寸和名称。

data.frame(matrix(as.numeric(l), attributes(l)$dim, dimnames=attributes(l)$dimnames))
#                      PC1         PC2
# Sepal.Length  0.59593180 -0.24252635
# Sepal.Width  -0.04181096 -0.96087188
# Petal.Length  0.56955777  0.11438157
# Petal.Width   0.56455387  0.06944826

Data 数据

iris_pca <- prcomp(iris[1:4], center=TRUE, scale.=TRUE)
l <- varimax(iris_pca$rotation[, 1:2])$loadings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM