title: How do I keep id variable in pca analysis in r after running factor loadings?
body: <p>I am trying to run social capital data through a principal components analysis (pca) in r by using the following dataset: <a href="https://aese.psu.edu/nercrd/community/social-capital-resources/social-capital-variables-for-2014/social-capital-variables-spreadsheet-for-2014/view" rel="nofollow noreferrer">https://aese.psu.edu/nercrd/community/social-capital-resources/social-capital-variables-for-2014/social-capital-variables-spreadsheet-for-2014/view</a></p>
  
  <p>I run the analysis just fine, but I need to merge the factor loadings onto the original dataset for further analysis and presentation. I simply need to know how I can retain the id variables when I run a pca analysis so I can merge those onto the original dataset.</p>
  
  <p>I have standardized the data, and then run the code below (which I gleaned from another source). I receive what appears to be factor loadings in one column for each county in the United States, but my problem is that my original database contains the id variables for each county (FIPS codes) but the factor loadings do not.</p>
  
  <pre><code>calcpc &lt;- function(variables,loadings)
  {
    # find the number of samples in the data set
    as.data.frame(variables)
    numsamples &lt;- nrow(variables)
    # make a vector to store the component
    pc &lt;- numeric(numsamples)
    # find the number of variables
    numvariables &lt;- length(variables)
    # calculate the value of the component for each sample
    for (i in 1:numsamples)
    {
      valuei &lt;- 0
      for (j in 1:numvariables)
      {
        valueij &lt;- variables[i,j]
        loadingj &lt;- loadings[j]
        valuei &lt;- valuei + (valueij * loadingj)
      }
      pc[i] &lt;- valuei
    }
    return(pc)
  }
  xxx&lt;-calcpc(standardisedconcentrations, socialcapital.pca$rotation[,1])
  </code></pre>