Written and maintained by CASRAI Editorial Board
Last updated
To run PCA in R, call prcomp() from the base stats package on a numeric data frame with scale. = TRUE, then read the sdev, rotation and x components of the result. No installation is needed: prcomp() ships with R. This guide walks through that workflow, explains how to read each part of the output, and documents where prcomp() and its older sibling princomp() differ by default, which is the point most tutorials skip.
What PCA in R is used for in research
Principal component analysis rewrites a set of correlated numeric variables as uncorrelated components ordered by the variance they carry. Researchers use it to reduce many measured variables to a few summary scores, to inspect structure in high-dimensional data, and to build composite predictors. If your aim is to model latent constructs behind survey items rather than to summarize variance, see exploratory factor analysis, which answers a different question. The same analysis in other packages is covered in the guides on principal component analysis in Stata and principal component analysis in SPSS.
Install or access
Both prcomp() and princomp() are part of the stats package that is attached in every R session, so there is nothing to install. The optional factoextra package on CRAN (version 2.2.0, published 2026-07-24, requires R 4.1.0 or later and ggplot2 3.5.2 or later) adds ggplot2-based scree plots and other plots for PCA results. Install it from CRAN with install.packages("factoextra").
Step-by-step: PCA with prcomp()
The default call in the R manual is prcomp(x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL, rank. = NULL, ...). Note the trailing dot in scale..
- Prepare a numeric data frame. Keep only the numeric columns you want to analyze, for example
X <- my_data[, c("var1", "var2", "var3")]. Remove or fix missing values first, or use the formula method described under troubleshooting. - Run the analysis with scaling.
pca <- prcomp(X, center = TRUE, scale. = TRUE).center = TRUEis already the default;scale. = FALSEis the default, and the R manual says that “in general scaling is advisable.” Scaling to unit variance means variables measured in large units do not dominate the components. - Look at the component standard deviations.
pca$sdevgives the standard deviation of each component. Square it to get variances:pca$sdev^2. These are the eigenvalues of the correlation matrix when you scaled, or of the covariance matrix when you did not. - Compute the share of variance per component.
pca$sdev^2 / sum(pca$sdev^2), and its running total withcumsum(). - Inspect the loadings.
pca$rotationholds the eigenvectors, one component per column. - Inspect the scores.
head(pca$x)holds your observations rotated into component space, returned becauseretx = TRUEis the default. - Draw a scree plot.
plot(pca)uses the plot method forprcompobjects, andscreeplot(pca)is the dedicated function. For how to judge where the curve bends, see the guide to the scree plot. - Draw a biplot.
biplot(pca)shows observations and variable directions together. - Project new observations.
predict(pca, newdata = new_X)applies the stored centering and scaling from the original fit to new rows, so do not scale the new data yourself.
Output and how to read it
| Component of the result | What it contains | How to use it |
|---|---|---|
sdev |
Standard deviations of the components; the square roots of the eigenvalues of the covariance or correlation matrix | Square to get eigenvalues; use for variance explained and scree plots |
rotation |
The variable loadings; the eigenvectors as columns | Read down a column to see which variables define that component |
x |
The data rotated into component space; cov(x) is diag(sdev^2) |
Use the columns as component scores in later analyses |
center, scale |
The centering and scaling values used | Reused automatically by predict() |
Because cov(x) is diagonal, the score columns are uncorrelated with each other and the variance of each equals the squared sdev entry. This is a quick check that you are reading the right object. When you use component scores as variables in a later model, read the guide on factor scores and when not to use them first.
prcomp() versus princomp(): what differs by default
R has two built-in PCA functions. Both are documented in the R manual, and their defaults differ in ways that change the numbers you get.
| Feature | prcomp() |
princomp() |
|---|---|---|
| Scaling or correlation default | scale. = FALSE (covariance-based unless you set it) |
cor = FALSE (covariance matrix; cor = TRUE uses the correlation matrix) |
| Calculation | Singular value decomposition of the data matrix, which the manual calls “generally the preferred method for numerical accuracy” | Eigen decomposition of the covariance matrix by default; the manual notes that a preferred method is to use SVD, as prcomp() does |
| Divisor for variances | N – 1 | N for the covariance matrix |
| Loadings object | rotation |
loadings |
| Scores object | x (if retx = TRUE) |
scores (if scores = TRUE) |
The practical consequence: the same data run through both functions gives slightly different standard deviations because of the N versus N – 1 divisor, even when both use the same scaling choice. If you compare R output with another package, check that package’s divisor and scaling defaults before concluding that something is wrong.
Troubleshooting
Error when scale. = TRUE
The manual states that scale = TRUE cannot be used if there are zero or constant variables. Find them with sapply(X, sd) == 0 and drop those columns. For princomp(), cor = TRUE has the same restriction.
Loadings have the opposite sign from another program or run
The manual says that the signs of the columns of the rotation matrix are arbitrary and can differ between programs and builds. Flipping the sign of a component, and of its scores, changes nothing about the analysis. Do not interpret a sign difference as a different result.
Missing values
prcomp() also has a formula method, prcomp(formula, data = NULL, subset, na.action, ...), whose na.action argument controls what happens to rows with missing values. For example prcomp(~ var1 + var2 + var3, data = my_data, scale. = TRUE).
Too many components
Two arguments limit what is returned. rank. sets the maximal number of components, and tol omits components whose standard deviation is less than or equal to tol times the standard deviation of the first component.
Free alternatives
| Option | What it is | Source |
|---|---|---|
prcomp() |
Base R, SVD-based PCA | R manual, stats package |
princomp() |
Base R, eigen-based PCA with cor option |
R manual, stats package |
factoextra |
CRAN package, GPL-2 licence, for extracting and visualizing multivariate results with ggplot2 | CRAN package page |
FAQ
Should I use prcomp() or princomp() in R?
Use prcomp(). The R manual describes SVD, which prcomp() uses, as generally preferred for numerical accuracy, and the princomp() page itself points to the SVD approach as preferred.
Should I scale the variables?
Usually yes. prcomp() does not scale by default, and its manual says scaling is in general advisable.
What is the difference between rotation and x?
rotation is the loading matrix (variables by components); x contains the scores (observations by components).
How do I get PCA scores for new data?
Call predict(pca, newdata), which uses the centering and scaling stored in the fitted object.
Sources
- R Core Team, R manual,
prcomp(stats package): stat.ethz.ch/R-manual/R-devel/library/stats/html/prcomp.html, checked 2026-10-29. - R Core Team, R manual,
princomp(stats package): stat.ethz.ch/R-manual/R-devel/library/stats/html/princomp.html, checked 2026-10-29. - CRAN, factoextra package page: cran.r-project.org/web/packages/factoextra/index.html, checked 2026-10-29.








