title: Dynamically selecting principal components from the PCA output
body: <p>This seems a trivial problem but i am unable to get the issue resolved!</p>
  
  <p>I have taken numeric columns of iris data set ..then normalized it as below</p>
  
  <pre><code>newiris&lt;-iris[,1:4]
  iris.norm&lt;-data.frame(scale(newiris))
  head(iris.norm)
    Sepal.Length Sepal.Width Petal.Length Petal.Width
  1   -0.8976739  1.01560199    -1.335752   -1.311052
  2   -1.1392005 -0.13153881    -1.335752   -1.311052
  3   -1.3807271  0.32731751    -1.392399   -1.311052
  4   -1.5014904  0.09788935    -1.279104   -1.311052
  5   -1.0184372  1.24503015    -1.335752   -1.311052
  6   -0.5353840  1.93331463    -1.165809   -1.048667
  
  # performed PCA now
  pccomp &lt;- prcomp(iris.norm )
  summary(pccomp)
  a &lt;- summary(pccomp)
  df&lt;- as.data.frame(a$importance)
  df &lt;- t(df)
  df
  ##     Standard deviation Proportion of Variance Cumulative Proportion
  ## PC1          1.7083611                0.72962               0.72962
  ## PC2          0.9560494                0.22851               0.95813
  ## PC3          0.3830886                0.03669               0.99482
  ## PC4          0.1439265                0.00518               1.00000
  </code></pre>
  
  <p>Now converting rownames into a column for df so that PCs which were rownames forms the first column for further manipulation</p>
  
  <pre><code>   library(tibble)
     library(dplyr)
     df&lt;-rownames_to_column(as.data.frame(df), var="PrinComp") %&gt;% head
     df
     ##   PrinComp Standard deviation Proportion of Variance Cumulative Proportion
     ## 1      PC1          1.7083611                0.72962               0.72962
     ## 2      PC2          0.9560494                0.22851               0.95813
     ## 3      PC3          0.3830886                0.03669               0.99482
     ## 4      PC4          0.1439265                0.00518               1.00000
  
   # Now will be selecting only those PCs where the cumulative proportion is say less than 96%
  # subsetting
  pcs&lt;-as.vector(as.character(df[which(df$`Cumulative Proportion`&lt;0.96),][,1])) # cumulative prop less than 96%
  pcs
  ## [1] "PC1" "PC2"
  </code></pre>
  
  <p>Now i am creating a PC data frame statically of  vector scores from the first 2 principal components which we got from the above condition (cum prop&lt;0.96)</p>
  
  <pre><code> x1 &lt;- pccomp$x[,1]
   x2 &lt;- pccomp$x[,2]
   pcdf &lt;- cbind(x1,x2)
   head(pcdf)
  ##             x1         x2
  ## [1,] -2.257141 -0.4784238
  ## [2,] -2.074013  0.6718827
  ## [3,] -2.356335  0.3407664
  ## [4,] -2.291707  0.5953999
  ## [5,] -2.381863 -0.6446757
  ## [6,] -2.068701 -1.4842053
  </code></pre>
  
  <p>My issue is how can i create the above pc data frame dynamically once i know the no of PCs based on condition such as cumulative proportion say being less than 0.95??</p>