for loop with cor.test over many categories <p>I am trying to write a loop in R that will cycle through 3 different species to calculate the correlation between two continuous variables (Redness and VarNormAbund).</p> <p>My loop is running, but the output for each of the 3 species is the same, which makes me think the loop is getting stuck at the first species.</p> <pre><code> cor.test.redness&lt;-lapply(unique(test$Species), function(x){cor.test(test$Redness, test$VarNormAbund)}) </code></pre> <p>Data structure: Species are in the first column. I would like the loop to extract each species and do a cor.test between Redness and VarNormAbund. There should be an output for each category. So 3 outputs in the list.</p> <p>Am I missing an argument that tells the loop to do each Species? </p> <p>Also, is there any way to have the output be a <code>data.frame</code> instead of a list? </p> <p>Any advice would be appreciated, I do not have much experience with loops.</p> <pre><code>Species&lt;-c("A","B","C","A","B","C","A","B","C") Redness&lt;-c(1,1,1,2,2,2,3,3,3) VarNormAbund&lt;-c(1.6, 0,0,12.5,0,1,1.37, 2.74, 0) test&lt;-data.frame(Species, Redness, VarNormAbund) </code></pre> <p>cheers.</p>