hli*_*nee 7 r pca dataframe tibble
我正在尝试将“加载”类型的对象转换为 R 中的数据帧。但是,我尝试通过 as_tibble() 或 as.data.frame() 对其进行强制没有奏效。这是代码:
iris_pca <- prcomp(iris[1:4], center = TRUE, scale. = TRUE)
iris_pca$rotation[,1:2] %>%
varimax() %>%
.$loadings
Run Code Online (Sandbox Code Playgroud)
这打印出来:
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
Run Code Online (Sandbox Code Playgroud)
如何将这些数据放入数据框中?
从"loadings"对象中提取数值作为数值。将它们强制转换为矩阵。您将在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
Run Code Online (Sandbox Code Playgroud)
数据
iris_pca <- prcomp(iris[1:4], center=TRUE, scale.=TRUE)
l <- varimax(iris_pca$rotation[, 1:2])$loadings
Run Code Online (Sandbox Code Playgroud)