title: Error in for loop - attempt to select less than one element in integerOneIndex
body: <p>I'm trying to translate a C routine from an old sound synthesis program into R, but have indexing issues which I'm struggling to understand (I'm a beginner when it comes to using loops).</p>
  
  <p>The routine creates an exponential lookup table - the vector <code>exptab</code>:</p>
  
  <pre><code># Define parameters
  sinetabsize &lt;- 8192
  prop &lt;- 0.8
  BP &lt;- 10
  BD &lt;- -5
  BA &lt;- -1 
  
  # Create output vector
  exptab &lt;- vector("double", sinetabsize)
  
  # Loop
  while(abs(BD) &gt; 0.00001){
    BY = (exp(BP) -1) / (exp(BP*prop)-1)
    if (BY &gt; 2){
      BS = -1
    }
    else{
      BS = 1
    }
    if (BA != BS){
      BD = BD * -0.5
      BA = BS
      BP = BP + BD
    }
    if (BP &lt;= 0){
      BP = 0.001
    }
    BQ = 1 / (exp(BP) - 1)
    incr = 1 / sinetabsize
    x = 0
    stabsize = sinetabsize + 1
    for (i in (1:(stabsize-1))){
    x = x + incr
    exptab [[sinetabsize-i]] = 1 - (BQ * (exp(BP * x) - 1))
    }
  }
  </code></pre>
  
  <p>Running the code gives the error:</p>
  
  <pre><code>Error in exptab[[sinetabsize - i]] &lt;- 1 - (BQ * (exp(BP * x) - 1)) : 
    attempt to select less than one element in integerOneIndex
  </code></pre>
  
  <p>Which, I understand from looking at other posts, indicates an indexing problem. But, I'm finding it difficult to work out the exact issue.  </p>
  
  <p>I suspect the error may lie in my translation. The original C code for the last few lines is:</p>
  
  <pre><code>for (i=1; i &lt; stabsize;i++){
    x += incr;
    exptab[sinetabsize-i] =  1.0 - (float) (BQ*(exp(BP*x) - 1.0));
  }
  </code></pre>
  
  <p>I had thought the R code <code>for (i in (1:(stabsize-1)))</code> was equivalent to the C code <code>for (i=1; i&lt; stabsize;i++)</code> (i.e. the initial value of i is <code>i = 1</code>, the test is whether <code>i &lt; stabsize</code>, and the increment is +1). But now I'm not so sure.</p>
  
  <p>Any suggestions as to where I'm going wrong would be greatly appreciated!</p>