if (require("psych")) {
pca <- principal_components(mtcars[, 1:7], n = 2, rotation = "varimax")
# PCA extracted two components
pca
# assignment of items to each component
closest_component(pca)
# now we want to have sum scores for each component
get_scores(pca)
# compare to manually computed sum score for 2nd component, which
# consists of items "hp" and "qsec"
(mtcars$hp + mtcars$qsec) / 2
}
Run the code above in your browser using DataLab