title: Computing ranges within bins
body: <p>I'm trying to find the sum of each bin given a random vector, but the code is only returning the first element of the vector as 100.  How would I cycle through each of the elements in the vector <code>x</code>, check if it is range of bin <code>j</code>, and return the sum for each bin?</p>
  
  <p>I realize there are functions to do this in <code>R</code>, but I'm working on hard coding this specific example.</p>
  
  <pre><code># Sample data
  set.seed(1234)
  x &lt;- rnorm(100)
  
  
  S &lt;- range(x)
  a &lt;- range(x)[1]
  b &lt;- range(x)[2]
  J &lt;- 5    #bins
  h &lt;- (b - a)/J   #interval
  
  for (j in 1:J){
    for (n in 1:length(x)){
      ifelse(x[n] &gt; a + (j-1)*h &amp; (x[n] &lt;= a + j*h), n[j] &lt;- n[j] + 1, n[j] &lt;- n[j] + 0)
    }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&gt; n
  [1] 100  NA  NA  NA  NA
  </code></pre>
  
  <p>Desired Output:</p>
  
  <pre><code>&gt; n
  [1]  7 43 29 13  8
  </code></pre>