title: For Loop for Correlations
body: <p>I am wanting to get correlation values between two variables for each county. </p>
  
  <p>I have subset my data as shown below and get the appropriate value for the individual Adams county, but am now wanting to do the other counties: </p>
  
  <pre><code>    CorrData&lt;-read.csv("H://Correlation 
       Datasets/CorrelationData_Master_Regression.csv")
       CorrData2&lt;-subset(CorrData, CountyName=="Adams")
       dzCases&lt;-(cor.test(CorrData2$NumVisit, CorrData2$dzdx, 
        method="kendall"))
  dzCases
  </code></pre>
  
  <p>I am wanting to do a For Loop or something similar that will make the process more efficient, and so that I don't have write 20 different variable correlations for each of the 93 counties.</p>
  
  <p>When I run the following in R, it doesn't give an error, but it doesn't give me the response I was hoping for either. Rather than the Spearman's Correlation for  each county, it seems to be ignoring the loop portion and just giving me the correlation between the two variables for ALL counties. </p>
  
  <pre><code>    CorrData&lt;-read.csv("H:\\CorrelationData_Master_Regression.csv")
       for (i in CorrData$CountyName)
       {
       dzCasesYears&lt;-cor.test(CorrData$NumVisit, CorrData$dzdx, 
       method="spearman")
       }
  </code></pre>
  
  <p>A very small sample of my data looks similar to this: </p>
  
  <pre><code>CountyName  Year    NumVisits        dzdx
  Adams       2010    4.545454545      1.19           
  Adams       2011    20.83333333      0.20           
  Elmore      2010    26.92307692      0.24       
  Elmore      2011    0                0.61           
  Brown       2010    0               -1.16           
  Brown       2011    17.14285714     -1.28           
  Clark       2010    25              -1.02           
  Clark       2011    0                1.13           
  Cass        2010    17.85714286      0.50       
  Cass        2011    27.55102041      0.11
  </code></pre>
  
  <p>I have tried to find a similar example online, but am not having luck! </p>
  
  <p>Thank you in advance for all your help!</p>