Skip to main content
v2026.11,858 entries · CC-BY 4.0

PCA in R: prcomp() Step by Step, Scaling, Loadings and Scores

Run PCA in R with prcomp(): scale, read sdev, rotation and x, plot a scree plot, and see how prcomp() differs from princomp().

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..

  1. 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.
  2. Run the analysis with scaling. pca <- prcomp(X, center = TRUE, scale. = TRUE). center = TRUE is already the default; scale. = FALSE is 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.
  3. Look at the component standard deviations. pca$sdev gives 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.
  4. Compute the share of variance per component. pca$sdev^2 / sum(pca$sdev^2), and its running total with cumsum().
  5. Inspect the loadings. pca$rotation holds the eigenvectors, one component per column.
  6. Inspect the scores. head(pca$x) holds your observations rotated into component space, returned because retx = TRUE is the default.
  7. Draw a scree plot. plot(pca) uses the plot method for prcomp objects, and screeplot(pca) is the dedicated function. For how to judge where the curve bends, see the guide to the scree plot.
  8. Draw a biplot. biplot(pca) shows observations and variable directions together.
  9. 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about PCA in R: prcomp() Step by Step, Scaling, Loadings and Scores

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.