# When using the prcomp function in R, what is the difference between the x values and the rotation values?

The prcomp function in R returns a class containing the following components:

• sdev: I'm not sure what these are, but I know that squaring them gives the eigenvalues.
• rotation: The above documentation states that this is "the matrix of variable loadings (i.e., a matrix whose columns contain the eigenvectors)".
• x: My understanding is that these are the principal components.

My understanding is that the principal components are the elements of the eigenvectors. If so, then this would mean that the x are the elements of the columns of rotation, right? But wouldn't this means that the vectors x and rotation are the same? Or am I misunderstanding this?

• pca$x <-data %*% pca$rotation Commented Jan 25, 2023 at 7:23

"rotation" are the principal components (the eigenvectors of the covariance matrix), in the original coordinate system. Typically a square matrix (unless you truncate it by introducing tolerance) with the same number of dimensions your original data had. E.g. if you had a 3D data set, your rotation matrix will be 3-by-3.

"x" is your data set projected on the principal components ("rotated"). It has the same dimensions as your original data set (again, assuming you don't truncate low-ranking PCs).

Here is an example: Assume your data set looks like this:

(the red and the blue line are the first and the second principal components, respecitvely).

After performing the PCA, you can plot it in terms of its PCs:

And here is the code, for reproducibility:

library(tidyverse)
theme_set(theme_bw())

set.seed(1)
N = 1000
X   = matrix(c(rnorm(N, 0, 3), rnorm(N, 0, 1)), ncol=2)
phi = 30/180*pi
M = matrix(c(cos(phi), sin(phi), -sin(phi), cos(phi)), nrow=2, byrow=T)
X = X %*% M
p = prcomp(X, center=T)

p

X %>% as_tibble %>% ggplot(aes(V1, V2)) +
geom_point(size=.5) +
xlim(-15, 15) + ylim(-15, 15) +
geom_abline(slope=p$$rotation[1, 2] / p$$rotation[1, 1], colour="red") +
geom_abline(slope=p$$rotation[2, 2] / p$$rotation[2, 1], colour="blue") +
coord_fixed(ratio = 1)

p$x %>% as_tibble %>% ggplot(aes(PC1, PC2)) + geom_point(size=.5) + xlim(-15, 15) + ylim(-15, 15) + geom_hline(yintercept = 0, colour="red") + geom_vline(xintercept = 0, colour="blue") + coord_fixed(ratio = 1)  Briefly Principal components have what is called "loadings" and "scores". Loadings specify the weight that each variable contributes to the principal component. Scores show the value each sample has on each principal component. So in the case of prcomp() function rotation are the loadings and x are the scores. Example Let's take example of using prcomp() on USArrests dataset. pca <- prcomp(USArrests)  Notice that pca$rotation has as many values per component as there are variables in the USArrests data. And pca$x has as many values per component as there are cases (states). pr$x is simply derived by applying the principal component loadings (rotation) to the original data, and we can double check this:

dat <- data.matrix(USArrests)
dat <- sweep(dat, 2, colMeans(dat))  # center each variable
scr <- dat %*% pca$rotation  Now scr has our principal component scores. They are the same as the ones returned by the function: all.equal(pca$x, scr)