title: Idiomatic R code for partitioning a vector by an index and performing an operation on that partition
body: <p>I'm trying to find the idiomatic way in R to partition a numerical vector by some index vector, find the sum of all numbers in that partition and then divide each individual entry by that partition sum. In other words, if I start with this:   </p>
  
  <pre><code>df &lt;- data.frame(x = c(1,2,3,4,5,6), index = c('a', 'a', 'b', 'b', 'c', 'c'))
  </code></pre>
  
  <p>I want the output to create a vector (let's call it z): </p>
  
  <pre><code>c(1/(1+2), 2/(1+2), 3/(3+4), 3/(3+4), 5/(5+6), 6/(5+6))  
  </code></pre>
  
  <p>If I were doing this is SQL and could use window functions, I would do this: </p>
  
  <pre><code>select 
   x / sum(x) over (partition by index) as z 
  from df
  </code></pre>
  
  <p>and if I were using plyr, I would do something like this: </p>
  
  <pre><code>ddply(df, .(index), transform, z = x / sum(x))
  </code></pre>
  
  <p>but I'd like to know how to do it using the standard R functional programming tools like mapply/aggregate etc. </p>