title: Calculate sum in subset of dataframe
body: <p>I want to calculate <code>sum(df$columnD==1)</code>/(number of rows in each subset of <code>columnE</code>) but so far I cant even extract <code>sum(df$columnD==1)</code> within each subset of <code>columnE</code> in the following data frame:</p>
  <pre><code>set.seed(10)
  A &lt;- seq(from=1, to=100, by=1)
  B &lt;- runif(100, -5, 0.2)               # actually I have 900,000 rows
  C &lt;- runif(100, 0, 1)
  D &lt;- rbinom(100, 1, 0.3)
  df &lt;- NULL
  df$columnA &lt;- A
  df$columnB &lt;- B
  df$columnC &lt;- C
  df$columnD &lt;- D
  df &lt;- as.data.frame(df)
  df$columnE &lt;- cut(df$columnB, quantile(df$columnB,(0:10)/10), labels=FALSE,
                    include.lowest=TRUE) # https://www.portfolioprobe.com/2012/12/24/miles-of-iles/
  index &lt;- order(df$columnE, decreasing = F)
  df &lt;- df[index,]
  </code></pre>
  <p>I have tried the following and none works:</p>
  <pre><code>sum(df$columnD==1)[df$columnE==1]             # No
  df$columnE[df$columnE==1][sum(df$columnD==1)] # Trying to extract only from subset 1
  (sum(df$columnD==1)/sum(df$columnE==1))       # Nein
  </code></pre>
  <p>How do I get around this?</p>