Search code examples
rtidyrpca

Missing PC1 column to a PCA rotation


I'm trying to do PCA on the classic Iris data set. This is my code:

library(tidyverse)

x <- iris[,1:4] %>% as.matrix()
pca <- prcomp(x, scale. = TRUE)
summary(pca)

The component rotation has every value for Petal.Length, Petal.Width, etc. So I tried this:

as.data.frame(pca$rotation) %>% pivot_longer(-1, names_to = "components", values_to = "value")

I know there must be a mistake since pca$rotation is a matrix and somehow PC1 is left out. The expected result should be:

terms          value component 
   <chr>          <dbl> <chr>   
 1 Sepal.Length  0.521  PC1     
 2 Sepal.Width  -0.269  PC1    
 3 Petal.Length  0.580  PC1    
 4 Petal.Width   0.565  PC1    
 5 Sepal.Length -0.377  PC2    
 6 Sepal.Width  -0.923  PC2     
 7 Petal.Length -0.0245 PC2      
 8 Petal.Width  -0.0669 PC2      
 9 Sepal.Length  0.720  PC3      
10 Sepal.Width  -0.244  PC3      
11 Petal.Length -0.142  PC3      
12 Petal.Width  -0.634  PC3    
13 Sepal.Length  0.261  PC4     
14 Sepal.Width  -0.124  PC4    
15 Petal.Length -0.801  PC4      
16 Petal.Width   0.524  PC4

Solution

  • Your -1 specification of which variables to include left out the first column (you may have misread the row names, which are typically ignored by tidyverse machinery, as the first column). How about

    pca$rotation %>%
        as.data.frame() %>%
        rownames_to_column("term") %>%
        as_tibble() %>%    ## cosmetic
        pivot_longer(starts_with("PC"), 
                     names_to = "components", 
                     values_to = "value")
    

    ?