title: Summary count by multiple groups with condition in dplyr
body: <p>I have a dataframe that looks like this:</p>
  
  <pre><code>data &lt;- data.frame(a=c(1,1,0,0,0,0,1,1,1, 0), 
                 b=c("x","x","x","x","x","y","y","y","z","z"),
                 c=c(2, 1, 2, 3, 4, NA, 4, 2, 1, 1), 
                 d= c("s", "m", "l", "l", "l", "m", "m", "s", "s", "m"))
  </code></pre>
  
  <p>I would like to find a way to create a new variable, e, that is a sum of the values in c when a=1 for every combination of d and b. I've tried several options that aren't giving me what I'm looking for, for instance: </p>
  
  <pre><code>data &lt;- data %&gt;% 
      group_by(d, b) %&gt;% 
      summarise (e = sum(data$c[which(data$a=="x")]))
  </code></pre>
  
  <p>Something that ultimately looks like: </p>
  
  <pre><code>       d      b     e
  1      s      x     2
  2      m      x     1
  3      l      x     9
  4      m      y     4
  5      s      y     2
  6      s      z     1
  7      s      z     1
  </code></pre>
  
  <p>But unfortunately, I am only getting a constant e? Any help appreciated!</p>