title: Accurate proportions in dplyr contingency table
body: <p>I have been searching for a way to do this for days and haven't found a programatic solution. I am modeling tidy data in R using Dplyr. I need to be able to create a contingency table that includes proportions. I have followed the solution of several other posts, but with tidy data the calculated proportions are wrong. While I know that I can use prop.table to make these computations, I'd like to stay in Dplyr because of the rest of the work that needs to be done in my workflow. It's a complex survey and variables span many columns throughout, making a tidy approach optimal.</p>
  
  <p>I'll recreate the issue with some simulated data:</p>
  
  <pre><code>library(tidyverse)
  
  set.seed(0520)
  
  # Create a data frame with three brands
  # Simulated survey data for brand familiarity
  # A zero means respondent is unfamiliar with brand
  # A one means they are familiar with the brand
  
  brands &lt;- data.frame(
    a = rbinom(n = 100, size = 1, prob = .3),
    b = rbinom(n = 100, size = 1, prob = .6),
    c = rbinom(n = 100, size = 1, prob = .55)
  )
  
  # Create factors for each
  
  brands$a &lt;- factor(brands$a, labels = c("Unfamiliar", "Familiar"))
  brands$b &lt;- factor(brands$b, labels = c("Unfamiliar", "Familiar"))
  brands$c &lt;- factor(brands$c, labels = c("Unfamiliar", "Familiar"))
  
  brands %&gt;% 
    select(a:c) %&gt;% 
    gather(brand, score) %&gt;% 
    filter(score == "Familiar") %&gt;% 
    group_by(Brand = brand) %&gt;% 
    summarize(n = n()) %&gt;% 
    mutate(Prop = n/sum(n))
  </code></pre>
  
  <p>As you will note, this code calculates the proportion of respondents who were familiar with brand C as 46%, which is incorrect. It should be 57%.</p>
  
  <p>My current workaround is to kludge in the number of rows in the original table (100) in my mutate statement. But I'd love a programatic way to do this, if possible.</p>
  
  <p>Thanks in advance for pointing me in the right direction.</p>