Skip to content

Instantly share code, notes, and snippets.

@mick001
Created August 13, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mick001/9b8058dbd8ac410cab9a1d4e63aafdef to your computer and use it in GitHub Desktop.
Save mick001/9b8058dbd8ac410cab9a1d4e63aafdef to your computer and use it in GitHub Desktop.
Compute PCA in R with built-in prcomp function
#-------------------------------------------------------------------------------
# With built-in prcomp function:
# Compute PCA
pc <- prcomp(D, center = T, scale. = T)
# Eigenvalues
(pc$sdev^2)[1:20]
# Eigenvectors (loadings)
pc$rotation[, 1:20]
# New features (principal components, also called scores)
pc$x[, 1:20]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment