title: How to create for loop to calculate gini function for binned data in r?
body: <p>I'm having some difficulties trying to calculate the gini coefficient using binned census data, and would really appreciate any help.</p>
  
  <p>My data looks a little something like this (but with 14,000 observations of 13 variables).</p>
  
  <pre><code>location &lt;- c('A','B','C', 'D', 'E', 'F')  
  no_income &lt;- c(20, 1, 40, 79, 12, 2)
  income1 &lt;- c(13, 4, 56, 17, 9, 4)
  income2 &lt;- c(27, 39, 49, 12, 19, 0)
  income3 &lt;- c(0, 1, 4, 3, 27, 0)
  
  df &lt;- data.frame(location, no_income, income1, income2, income3)
  </code></pre>
  
  <p>So for each observation there is a location given, and then a series of columns indicating how many households in the area earn within the given income bracket (so for location A, 20 households earn $0, 13 earn income1, 27 income2, and 0 income3).</p>
  
  <p>I've created an empty column to return the results to:</p>
  
  <p><code>df$gini = 0</code></p>
  
  <p>I've then created a numerical vector (x) containing the income amount I want to use for each income bin </p>
  
  <pre><code>x &lt;- c(0, 300, 1000, 2000)
  </code></pre>
  
  <p>I've been trying to use the gini function within the reldist package, and have written the following for loop to cycle through each row of the data, apply the gini function and return the output to a new column.</p>
  
  <pre><code>for (i in 1:nrow(samp)){ 
       w &lt;- samp[i,2:5] 
       df$gini &lt;- gini(x, w=rep(1, length=length(x)))
       }
  </code></pre>
  
  <p>The problem is that the ouput returned is currently identical for each row, which is obviously not correct. I'm relatively new to this though, and not sure what I'm doing wrong...</p>