title: Using for loop to filter data frame with different groups in R
body: <p>If I have a data frame: </p>
  
  <pre><code>d &lt;- data.frame(
    name = c("n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "n10"),
    color = c("blue", "blue", "red", "blue", "red", "blue", "blue", "red", "green", "green"),
  weight = c(53, 34, 63, 25, 45, 24, 66, 12, 45, 8),
    gender = c(1, 0, 0, 0, 1 ,1 ,1 , 0, 1, 0)) 
  </code></pre>
  
  <p>How would I use the <code>for</code> loop function in R to filter 'weight' values that are +10 from the mean <strong>for each 'color</strong>'? So I would get an output of all the rows that had a weight +10 from the mean for each of the separate 'colours'. </p>
  
  <p>I know <code>d[d$weight &gt; mean(d$weight) + (10 + sd(d$weight)), ]</code> would give me the rows that fit this criteria of the entire sample, but I'm trying to find the values for each 'color' separately since each 'color' has a different sd value. </p>
  
  <p>I'm trying to understand the <code>for</code> loops in R. </p>