title: ddply summarise proportional count
body: <p>I am having some trouble using the ddply function from the plyr package. I am trying to summarise the following data with counts and proportions within each group. Here's my data:</p>
  
  <pre><code>    structure(list(X5employf = structure(c(1L, 3L, 1L, 1L, 1L, 3L, 
  1L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 
  2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 3L, 1L, 
  3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
  3L, 3L, 1L), .Label = c("increase", "decrease", "same"), class = "factor"), 
      X5employff = structure(c(2L, 6L, NA, 2L, 4L, 6L, 5L, 2L, 
      2L, 8L, 2L, 2L, 2L, 7L, 7L, 8L, 11L, 7L, 2L, 8L, 8L, 11L, 
      7L, 6L, 2L, 5L, 2L, 8L, 7L, 7L, 7L, 8L, 6L, 7L, 5L, 5L, 7L, 
      2L, 6L, 7L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 2L, 5L, 2L, 2L, 
      2L, 5L, 12L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 2L, 5L, 2L, 
      13L, 9L, 9L, 9L, 7L, 8L, 5L), .Label = c("", "1", "1  and 8", 
      "2", "3", "4", "5", "6", "6 and 7", "6 and 7 ", "7", "8", 
      "1 and 8"), class = "factor")), .Names = c("X5employf", "X5employff"
  ), row.names = c(NA, 73L), class = "data.frame")
  </code></pre>
  
  <p>And here's my call using ddply:</p>
  
  <pre><code>ddply(kano_final, .(X5employf, X5employff), summarise, n=length(X5employff), prop=(n/sum(n))*100)
  </code></pre>
  
  <p>This gives me the counts of each instance of <code>X5employff</code> correctly, but but seems as though the proportion is being calculated across each row and not within each level of the factor <code>X5employf</code> as follows:</p>
  
  <pre><code>   X5employf X5employff  n prop
  1   increase          1 26  100
  2   increase          2  1  100
  3   increase          3 15  100
  4   increase    1 and 8  1  100
  5   increase       &lt;NA&gt;  1  100
  6   decrease          4  1  100
  7   decrease          5  5  100
  8   decrease          6  2  100
  9   decrease          7  1  100
  10  decrease          8  1  100
  11      same          4  4  100
  12      same          5  6  100
  13      same          6  5  100
  14      same    6 and 7  3  100
  15      same          7  1  100
  </code></pre>
  
  <p>When manually calculating the proportions within each group I get this:</p>
  
  <pre><code>   X5employf X5employff  n prop
  1   increase          1 26  59.09
  2   increase          2  1  2.27
  3   increase          3 15  34.09
  4   increase    1 and 8  1  2.27
  5   increase       &lt;NA&gt;  1  2.27
  6   decrease          4  1  10.00
  7   decrease          5  5  50.00
  8   decrease          6  2  20.00
  9   decrease          7  1  10.00
  10  decrease          8  1  10.00
  11      same          4  4  21.05
  12      same          5  6  31.57
  13      same          6  5  26.31
  14      same    6 and 7  3  15.78
  15      same          7  1  5.26
  </code></pre>
  
  <p>As you can see the sum of proportions in each level of factor X5employf equals 100.</p>
  
  <p>I know this is probably ridiculously simple, but I can't seem to get my head around it despite reading all sorts of similar posts. Can anyone help with this and my understanding of how the summarise function works?!</p>
  
  <p>Many, many thanks</p>
  
  <p>Marty</p>