title: Vectorizing a loop in R
body: <p>There must be a simple way to vectorize the following loop in R, but I can't see it.</p>
  
  <pre><code>w &lt;- numeric(10)
  z &lt;- rnorm(20)
  v &lt;- c(sample(1:10,10),sample(1:10,10)) #Random ordering of c(1:10,1:10)
  for(i in 1:10)
      w[i] &lt;- sum(z[v==i])
  </code></pre>