Multilevel summarize with dplyr in R
<p>I have a set of data like this</p>

<pre><code>project,file,line,mutant,killstatus,commitid,isbug
p1,f1,100,NMC,killed,abc123,yes
p1,f1,100,VMC,alive,abc123,yes
p1,f1,200,NVM,alive,ab12de,no
p1,f1,200,NVM,alive,abcde1,yes
p1,f1,200,NVM,alive,abcde2,yes
</code></pre>

<p>I would like to group by project,file,line and summarize it as</p>

<pre><code>project,file,line,total.mutants,killed.mutants,total.commits,bugfix.commits
p1,f1,100,2,1,1,1
p1,f1,200,1,0,3,2
</code></pre>

<p>That is, for each <code>project,file,line</code> combination, the length of <code>total.mutants</code> is the unique count of <code>(mutant,killstatus)</code>, and <code>killed.mutants</code> is the count of <code>killed</code> in the previous pair. similarly, <code>total.commits</code> is the unique count of <code>(commitid,isbug)</code> and <code>bugfix.commits</code> is the count of <code>yes</code> in the previous pair.</p>

<p>To clarify: 
For <code>p1,f1,100</code> there are two unique mutant,killstatus pairs <code>(NMC,killed),(VMC,alive)</code> of which one is killed. Hence <code>2,1</code> for total, killed
For <code>p1,f1,200</code> there is only one unique mutant,killstatus pair <code>(NVM,alive)</code> which is alive. Hence <code>1,0</code> for total,killed</p>

<p>For <code>p1,f1,100</code> there is one unique commitid,isbug pair <code>(abc123,yes)</code> which is a bug. Hence <code>1,1</code> for total,bugfix
For <code>p1,f1,200</code> there are three unique commitid,isbug pairs <code>(ab12de,no),(abcde1,yes),(abcde2,yes)</code> of which two are bugs. Hence <code>3,2</code> for total,bugfix</p>

<p>Can I use dplyr to summarize it in one go? I managed to do the first partially like</p>

<pre><code>data %&gt;% group_by(project,file,line) %&gt;% summarize(
   total.mutants = length(killstatus),
   killed.mutants = sum(ifelse(killstatus == 'alive', F, T))
)
</code></pre>

<p>but I don't know how to do the unique part, and second part.</p>