title: dplyr to create aggregate percentages of factor levels
body: <p>How do I use dplyr to create proportions of a level of a factor variable for each state? For example, I'd like to add a variable that indicates the percent of females within each state to the data frame. </p>
  
  <pre><code># gen data
  state &lt;- rep(c(rep("Idaho", 10), rep("Maine", 10)), 2)
  student.id &lt;- sample(1:1000,8,replace=T)
  gender &lt;- rep( c("Male","Female"), 100*c(0.25,0.75) )  
  gender &lt;- sample(gender, 40)
  school.data &lt;- data.frame(student.id, state, gender)
  </code></pre>
  
  <p>Here's an attempt that I know is wrong, but gets me access to the information:</p>
  
  <pre><code> middle %&gt;%
     group_by(state, gender %in%c("Female")) %&gt;%
     summarise(count = n()) %&gt;%
     mutate(test_count = count)
  </code></pre>
  
  <p>I have a hard time with the count and mutate functions, which makes it hard to get much further. It doesn't behave as I'd expect. </p>