title: group-wise summaries/subsets dplyr
body: <p>I have a data set of two courses in 2 different semesters that takes the following form: </p>
  
  <pre><code>set.seed(200)
  sem &lt;- sample(c("1", "2"), 200, replace = T)
  course &lt;- sample(c("1", "2"), 200, replace = T)
  d.gender = sample(c(0, 1), 200, replace = T, prob = c(0.6, 0.4))
  d.pass = sample(c(0, 1), 200, replace = T, prob = c(0.7, 0.3))
  df &lt;- data.frame(sem, course, d.gender, d.pass)
  </code></pre>
  
  <p>I'm trying to <em>efficiently</em> create a tbl of the 4 different sem,course combinations along with their total pass rate, the percentage of d.gender = 1, and finally the pass rates within those the 2 gender categories. I can make a table that provides all the values I need to calculate, but I know there's a more efficient way to calculate what I need without nesting a bunch of different group_by and summary functions, or making a whole bunch of different tbls and left_joining the columns I want. I can get what I need grinding away with indices and subset functions, but I'm hopeful that there is a better way to get a 4-row matrix with everything I need but it's ugly and takes forever, and it's easy to make mistakes in the code:</p>
  
  <pre><code>df1 &lt;- df %&gt;% group_by(sem, course, d.gender, d.pass) %&gt;% summarize(total = n())
  df1$total_pass &lt;- rep(NA, dim(df1)[1])
  df1$total_pass[1:4] &lt;- sum(subset(df1, sem == "1" &amp; course == "1" &amp; d.pass == "1", 
      select = total))
  df1$total_pass[5:8] &lt;- sum(subset(df1, sem == "1" &amp; course == "2" &amp; d.pass == "1", 
      select = total))
  df1$total_pass[9:12] &lt;- sum(subset(df1, sem == "2" &amp; course == "1" &amp; d.pass == "1", 
      select = total))
  df1$total_pass[13:16] &lt;- sum(subset(df1, sem == "2" &amp; course == "2" &amp; d.pass == "1", 
      select = total))
  
  df1$n_male &lt;- rep(NA, dim(df1)[1])
  df1$n_male[1:4] &lt;- sum(subset(df1, sem == "1" &amp; course == "1" &amp; d.gender == "1", 
      select = total))
  df1$n_male[5:8] &lt;- sum(subset(df1, sem == "1" &amp; course == "2" &amp; d.gender == "1", 
      select = total))
  df1$n_male[9:12] &lt;- sum(subset(df1, sem == "2" &amp; course == "1" &amp; d.gender == "1", 
      select = total))
  df1$n_male[13:16] &lt;- sum(subset(df1, sem == "2" &amp; course == "2" &amp; d.gender == "1", 
      select = total))
  
  df1$n_fem &lt;- rep(NA, dim(df1)[1])
  df1$n_fem[1:4] &lt;- sum(subset(df1, sem == "1" &amp; course == "1" &amp; d.gender == "0", select = total))
  df1$n_fem[5:8] &lt;- sum(subset(df1, sem == "1" &amp; course == "2" &amp; d.gender == "0", select = total))
  df1$n_fem[9:12] &lt;- sum(subset(df1, sem == "2" &amp; course == "1" &amp; d.gender == "0", 
      select = total))
  df1$n_fem[13:16] &lt;- sum(subset(df1, sem == "2" &amp; course == "2" &amp; d.gender == "0", 
      select = total))
  
  df1$pct_male &lt;- rep(NA, dim(df1)[1])
  df1$pct_male[1:4] &lt;- df1$n_male[1:4]/sum(subset(df1, sem == "1" &amp; course == "1", 
      select = total))
  df1$pct_male[5:8] &lt;- df1$n_male[5:8]/sum(subset(df1, sem == "1" &amp; course == "2", 
      select = total))
  df1$pct_male[9:12] &lt;- df1$n_male[9:12]/sum(subset(df1, sem == "2" &amp; course == "1", 
      select = total))
  df1$pct_male[13:16] &lt;- df1$n_male[13:16]/sum(subset(df1, sem == "2" &amp; course == "2", 
      select = total))
  
  df1$pct_fem &lt;- rep(NA, dim(df1)[1])
  df1$pct_fem &lt;- 1 - df1$pct_male
  
  df1$pct_pass &lt;- rep(NA, dim(df1)[1])
  df1$pct_pass[1:4] &lt;- df1$total_pass[1:4]/sum(subset(df1, sem == "1" &amp; course == "1", 
      select = total))
  df1$pct_pass[5:8] &lt;- df1$total_pass[5:8]/sum(subset(df1, sem == "1" &amp; course == "2", 
      select = total))
  df1$pct_pass[9:12] &lt;- df1$total_pass[9:12]/sum(subset(df1, sem == "2" &amp; course == 
      "1", select = total))
  df1$pct_pass[13:16] &lt;- df1$total_pass[13:16]/sum(subset(df1, sem == "2" &amp; course == 
      "2", select = total))
  
  df1$male_pass_pct &lt;- rep(NA, dim(df1)[1])
  df1$male_pass_pct[1:4] &lt;- subset(df1, sem == "1" &amp; course == "1" &amp; d.gender == "1" &amp; 
      d.pass == "1", select = total)/df1$n_male[1:4]
  df1$male_pass_pct[5:8] &lt;- subset(df1, sem == "1" &amp; course == "2" &amp; d.gender == "1" &amp; 
      d.pass == "1", select = total)/df1$n_male[5:8]
  df1$male_pass_pct[9:12] &lt;- subset(df1, sem == "2" &amp; course == "1" &amp; d.gender == "1" &amp; 
      d.pass == "1", select = total)/df1$n_male[9:12]
  df1$male_pass_pct[13:16] &lt;- subset(df1, sem == "2" &amp; course == "2" &amp; d.gender == 
      "1" &amp; d.pass == "1", select = total)/df1$n_male[13:16]
  
  df1$fem_pass_pct &lt;- rep(NA, dim(df1)[1])
  df1$fem_pass_pct[1:4] &lt;- subset(df1, sem == "1" &amp; course == "1" &amp; d.gender == "0" &amp; 
      d.pass == "1", select = total)/df1$n_fem[1:4]
  df1$fem_pass_pct[5:8] &lt;- subset(df1, sem == "1" &amp; course == "2" &amp; d.gender == "0" &amp; 
      d.pass == "1", select = total)/df1$n_fem[5:8]
  df1$fem_pass_pct[9:12] &lt;- subset(df1, sem == "2" &amp; course == "1" &amp; d.gender == "0" &amp; 
      d.pass == "1", select = total)/df1$n_fem[9:12]
  df1$fem_pass_pct[13:16] &lt;- subset(df1, sem == "2" &amp; course == "2" &amp; d.gender == "0" &amp; 
      d.pass == "1", select = total)/df1$n_fem[13:16]
  
  
  df2 &lt;- df1 %&gt;% 
      group_by(sem, course) %&gt;% 
      summarize(total_pass = first(total_pass), 
                pct_pass = first(pct_pass), 
                n_male = first(n_male), 
                n_fem = first(n_fem), 
                pct_male = first(pct_male), 
                pct_fem = first(pct_fem), 
                male_pass_pct = first(male_pass_pct), 
                fem_pass_pct = first(fem_pass_pct))
  
  df2 &lt;- unique(df1[, c(1, 2, 6, 11, 7:10, 12, 13)])
  df2[, c(9, 10)] &lt;- lapply(df2[, c(9, 10)], as.numeric)
  </code></pre>
  
  <p>that's really laborious for only needing measures for 4 rows, but I can't get it to work otherwise for this aggregation... Any help would be awesome</p>