title: Aggregate (count) rows that match a condition, group by unique values
body: <p>It seems like such a simple problem, yet i've been pulling my hair out trying to get this to work:</p>
  
  <p>Given this data frame identifying the interactions <code>id</code>had with <code>contact</code> who is grouped by <code>contactGrp</code>,</p>
  
  <pre><code>head(data)
     id               sesTs  contact    contactGrp   relpos   maxpos
  1 6849 2012-06-25 15:58:34   peter        west    0.000000      3
  2 6849 2012-06-25 18:24:49   sarah        south   0.500000      3
  3 6849 2012-06-27 00:13:30   sarah        south   1.000000      3
  4 1235 2012-06-29 17:49:35   peter        west    0.000000      2
  5 1235 2012-06-29 23:56:35   peter        west    1.000000      2
  6 5893 2012-06-30 22:21:33   carl         east    0.000000      1
  </code></pre>
  
  <p>how many contacts where there for <code>unique(data$contactGrp)</code> with <code>relpos=1</code> and <code>maxpos&gt;1</code> ?</p>
  
  <p>An expected Result would be:</p>
  
  <pre><code>1 west   1
  2 south  1
  3 east   0
  </code></pre>
  
  <p>A small subset of lines i have tried:</p>
  
  <ul>
  <li><code>aggregate(data, by=list('contactGrp'), FUN=count)</code> yields an error, no filtering</li>
  <li>using <code>data.table</code> seems to require a key, which is not unique in this data…</li>
  <li><code>ddply(data,"contactGrp",summarise,count=???)</code> not sure which function to use to fill the <code>count</code> column</li>
  <li><code>ddply(subset(data,maxpos&gt;1 &amp; relpos==0), c('contactGrp'), function(df)count(df$relpos))</code> works but gives me an extra column <code>x</code> and it feels like i've overcomplicated it…</li>
  </ul>
  
  <p>SQL would be easy: <code>Select contactGrp, count(*) as cnt from data where … Group by contactGrp</code> but im trying to learn <code>R</code></p>