title: R programming: plyr how to count values from a column with ddply
body: <p>I would like to summarize the pass/fail status for my data as below. In other words, I would like to tell the number of pass and fail cases for each product/type.</p>
  
  <pre><code>library(ggplot2)
  library(plyr)
  product=c("p1","p1","p1","p1","p1","p1","p1","p1","p1","p1","p1","p1","p2","p2","p2","p2","p2","p2","p2","p2","p2","p2","p2","p2")
  type=c("t1","t1","t1","t1","t1","t1","t2","t2","t2","t2","t2","t2","t1","t1","t1","t1","t1","t1","t2","t2","t2","t2","t2","t2")
  skew=c("s1","s1","s1","s2","s2","s2","s1","s1","s1","s2","s2","s2","s1","s1","s1","s2","s2","s2","s1","s1","s1","s2","s2","s2")
  color=c("c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3","c1","c2","c3")
  result=c("pass","pass","fail","pass","pass","pass","fail","pass","fail","pass","fail","pass","fail","pass","fail","pass","pass","pass","pass","fail","fail","pass","pass","fail")
  df = data.frame(product, type, skew, color, result)
  </code></pre>
  
  <p>The following cmd returns the total number of pass+fail cases but I want separate columns for pass and fail</p>
  
  <pre><code>dfSummary &lt;- ddply(df, c("product", "type"), summarise, N=length(result))
  </code></pre>
  
  <p>Result is:</p>
  
  <pre><code>        product type N
   1      p1      t1   6
   2      p1      t2   6
   3      p2      t1   6
   4      p2      t2   6
  </code></pre>
  
  <p>The desireable result would be </p>
  
  <pre><code>         product type Pass Fail
   1       p1      t1   5    1
   2       p1      t2   3    3
   3       p2      t1   4    2
   4       p2      t2   3    3
  </code></pre>
  
  <p>I have attempted somthing like: </p>
  
  <pre><code> dfSummary &lt;- ddply(df, c("product", "type"), summarise, Pass=length(df$product[df$result=="pass"]), Fail=length(df$product[df$result=="fail"]) )
  </code></pre>
  
  <p>but obviously it’s wrong since the results are the grand totatl for fail and pass.</p>
  
  <p>Thanks in advance for your advice !
  Regards,
  Riad.</p>