Export PCA components in r

8.7k views Asked by At

I did pca on my data using r and I am trying to save the components with an eigenvalue larger than 1.

> summary(pca1)
Importance of components:
                          Comp.1    Comp.2    Comp.3    Comp.4    Comp.5     Comp.6     Comp.7     Comp.8
Standard deviation     1.2851803 1.1245020 1.0737268 1.0011978 0.9841687 0.88758402 0.84798807 0.67308490
Proportion of Variance 0.2064611 0.1580631 0.1441112 0.1252996 0.1210735 0.09847567 0.08988547 0.05663041
Cumulative Proportion  0.2064611 0.3645241 0.5086353 0.6339349 0.7550084 0.85348412 0.94336959 1.00000000
> loadings(pca1)

Loadings:
                            Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8
AspectRatio                  0.604         0.325                0.230  0.194  0.652
CPUSpeed                     0.241                0.278  0.890 -0.242              
IsDvrEnabled                 0.428        -0.329 -0.109 -0.290 -0.724 -0.281       
ZoomMode                     0.123         0.837        -0.133 -0.232 -0.124 -0.432
Tuner_BitRate                0.600        -0.272                0.392  0.161 -0.616
Tuner_Hole                                       -0.948  0.306                     
Receiver_VideoDecoderErrors        -0.705                       0.283 -0.640       
Receiver_AudioDecoderErrors -0.128 -0.690                      -0.275  0.650       

               Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8
SS loadings     1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000
Proportion Var  0.125  0.125  0.125  0.125  0.125  0.125  0.125  0.125
Cumulative Var  0.125  0.250  0.375  0.500  0.625  0.750  0.875  1.000

So in this case I am interested in the first four components. Is there a way that I can save it in a table or a file (file is proffered). Thank you!

1

There are 1 answers

0
Vlo On BEST ANSWER

loadings(pca1) returns the PCA Loadings. unclass drops the class and converts it into a matrix.

pca1$sdev^2 > 1 returns TRUE for columns where the eigenvalue > 1. [...,drop = F] selects the columns where the index is equals to TRUE while keeping the matrix structure even when only one column is selected. write.csv writes the results to a file.

Final Code: write.csv(x = unclass(loadings(pca1))[,(pca1$sdev^2 > 1),drop = FALSE], file = "filename.csv")