title: How to correlate multiple subsets in R
body: <p>How do I correlate 8 subsets separately against two different dependent variables? I keep getting the same correlation coefficient for the two different subsets (example below). Here is the input:</p>
  <pre><code>with(subset(mydata2, PARTYID_Strength = 1), cor.test(PARTYID_Strength,
                                                       mean.legit))
  
  with(subset(mydata2, PARTYID_Strength = 1), cor.test(PARTYID_Strength,
                                                       mean.leegauthor))
  
  with(subset(mydata2, PARTYID_Strength = 2), cor.test(PARTYID_Strength,
                                                       mean.legit))
  
  with(subset(mydata2, PARTYID_Strength = 2), cor.test(PARTYID_Strength,
                                                       mean.leegauthor))
  </code></pre>
  <p>Output (I get this for both PARTY_Strength = 1 and 2):</p>
  <blockquote>
  <p>Pearson's product-moment correlation</p>
  <p>data:  PARTYID_Strength and mean.legit   t = 3.1005, df = 607, p-value
  = 0.002022   alternative hypothesis: true correlation is not equal to 0   95 percent confidence interval:<br />
  0.0458644 0.2023031   sample estimates:<br />
  cor<br />
  0.1248597</p>
  <p>Pearson's product-moment correlation</p>
  <p>data:  PARTYID_Strength and mean.leegauthor   t = 2.8474, df = 607,
  p-value = 0.004557   alternative hypothesis: true correlation is not
  equal to 0   95 percent confidence interval:<br />
  0.03568431 0.19250344   sample estimates:<br />
  cor<br />
  0.1148091</p>
  </blockquote>
  <p>Sample data:</p>
  <pre><code>&gt; dput(head(mydata2, 10))
  ``structure(list(PARTYID = c(1, 3, 1, 1, 1, 4, 3, 1, 1, 1), PARTYID_Other = 
  c(&quot;NA&quot;, 
  &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;Green&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;), PARTYID_Strength = 
  c(1, 
  7, 1, 2, 1, 8, 1, 6, 1, 1), PARTYID_Strength_Other = c(&quot;NA&quot;, 
  &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;Green&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;), THERM_Dem = c(80, 
  65, 85, 30, 76, 15, 55, 62, 90, 95), THERM_Rep = c(1, 45, 10, 
  5, 14, 14, 0, 4, 10, 3), Gender = c(&quot;Female&quot;, &quot;Male&quot;, &quot;Male&quot;, 
  &quot;Female&quot;, &quot;Female&quot;, &quot;Male&quot;, &quot;Male&quot;, &quot;Female&quot;, &quot;Female&quot;, &quot;Male&quot;
  ), `MEAN Age` = c(29.5, 49.5, 29.5, 39.5, 29.5, 21, 39.5, 39.5, 
  29.5, 65), Age = c(&quot;25 - 34&quot;, &quot;45 - 54&quot;, &quot;25 - 34&quot;, &quot;35 - 44&quot;, 
  &quot;25 - 34&quot;, &quot;18 - 24&quot;, &quot;35 - 44&quot;, &quot;35 - 44&quot;, &quot;25 - 34&quot;, &quot;65+&quot;), 
  Ethnicity = c(&quot;White or Caucasian&quot;, &quot;Asian or Asian American&quot;, 
  &quot;White or Caucasian&quot;, &quot;White or Caucasian&quot;, &quot;Hispanic or Latino&quot;, 
  &quot;White or Caucasian&quot;, &quot;White or Caucasian&quot;, &quot;White or Caucasian&quot;, 
  &quot;White or Caucasian&quot;, &quot;White or Caucasian&quot;), Ethnicity_Other = c(&quot;NA&quot;, 
  &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;, &quot;NA&quot;), States = c(&quot;Texas&quot;, 
  &quot;Texas&quot;, &quot;Ohio&quot;, &quot;Texas&quot;, &quot;Puerto Rico&quot;, &quot;New Hampshire&quot;, 
  &quot;South Carolina&quot;, &quot;Texas&quot;, &quot;Texas&quot;, &quot;Texas&quot;), Education = c(&quot;Master's 
  degree&quot;, 
  &quot;Bachelor's degree in college (4-year)&quot;, &quot;Bachelor's degree in college (4- 
   year)&quot;, 
  &quot;Master's degree&quot;, &quot;Master's degree&quot;, &quot;Less than high school degree&quot;, 
  &quot;Some college but no degree&quot;, &quot;Master's degree&quot;, &quot;Master's degree&quot;, 
  &quot;Some college but no degree&quot;), `MEAN Income` = c(30000, 140000, 
  150000, 60000, 80000, 30000, 30000, 120000, 150000, 60000
  ), Income = c(&quot;Less than $30,000&quot;, &quot;$130,001 to $150,000&quot;, 
  &quot;More than $150,000&quot;, &quot;$50,001 to $70,000&quot;, &quot;$70,001 to $90,000&quot;, 
  &quot;Less than $30,000&quot;, &quot;Less than $30,000&quot;, &quot;$110,001 to $130,000&quot;, 
  &quot;More than $150,000&quot;, &quot;$50,001 to $70,000&quot;), mean.partystrength = c(3.875, 
  2.875, 2.375, 3.5, 2.625, 3.125, 3.375, 3.125, 3.25, 3.625
  ), mean.traitrep = c(2.5, 2.625, 2.25, 2.625, 2.75, 1.875, 
  2.75, 2.875, 2.75, 3), mean.traitdem = c(2.25, 2.625, 2.375, 
  2.75, 2.625, 2.125, 1.875, 3, 2, 2.5), mean.leegauthor = c(1, 
  2, 2, 4, 1, 4, 1, 1, 1, 1), mean.legit = c(1.71428571428571, 
  3.28571428571429, 2.42857142857143, 2.42857142857143, 2.14285714285714, 
  1.28571428571429, 1.42857142857143, 1.14285714285714, 2.14285714285714, 
  1.28571428571429)), row.names = c(NA, -10L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;))``
  </code></pre>
  <p>Thank you!</p>