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. 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?
How can I get this data into a dataframe? From the "loadings" object extract the values as numeric. Coerce them into a matrix. Needed dimensions and names you will find within str (l).
Shows a loadings plot for selected components. ## S3 method for class 'pca' plotLoadings ( obj, comp = c (1, 2), type = (if (length (comp == 2)) "p" else "l"), show.legend = TRUE, show.axes = TRUE, ... ) a value or vector with several values - number of components to show the plot for
Each variable with any loading larger than 0.5 (in modulus) is assigned to the factor with the largest loading, and the variables are printed in the order of the factor they are assigned to, then those unassigned. … further arguments for other methods, ignored for loadings.
From the "loadings"
object extract the values as numeric. Coerce them into a matrix. Needed dimensions and names you will find within 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With