title: Julia DataFrame: Create new column sum of col values :x by :y
body: <p>I have a <code>DataFrame</code> of x and y occurrences. I would like to count how often each occurrence happens in the <code>DataFrame</code> and what percentage of the <code>:y</code> occurrences that combination represents. I have the first part down now, thanks to <a href="https://stackoverflow.com/questions/43948804/julia-create-summary-values-for-column-x-for-each-unique-value-in-column-y-of-d">a previous question</a>.</p>
  
  <pre><code>using DataFrames
  mydf = DataFrame(y = rand('a':'h', 1000), x = rand('i':'p', 1000))
  mydfsum = by(mydf, [:x, :y], df -&gt; DataFrame(n = length(df[:x])))
  </code></pre>
  
  <p>This successfully creates a column that counts how often each value of <code>:x</code> occurs with each value of <code>:y</code>. Now I need to be able to generate a new column that counts how often each value of <code>:y</code> occurs. I could next create a new <code>DataFrame</code> using:</p>
  
  <pre><code>mydfsumy = by(mydf, [:y], df -&gt; DataFrame(ny = length(df[:x])))
  </code></pre>
  
  <p>Join the <code>DataFrames</code> together.</p>
  
  <pre><code>mydfsum = join(mydfsum, mydfsumy, on = :y)
  </code></pre>
  
  <p>And create the percentage <code>:yp</code> column</p>
  
  <pre><code>mydfsum[:yp] = mydfsum[:n] ./ mydfsum[:ny]
  </code></pre>
  
  <p>But this seems like a clunky workaround for a common data management problem. In R I would do all of this in one line using <code>dplyr</code>:</p>
  
  <pre><code>mydf %&gt;% groupby(x,y) %&gt;% summarize(n = n()) %&gt;% groupby(y) %&gt;% mutate(yp = n/sum(n))
  </code></pre>