title: For loop in R starting from other value
body: <p>This has been bothering me for some time now. Imagine you have to/want to use a loop.</p>
  
  <p>If you write a loop, but you want to start from the number 2, you'd use the following:</p>
  
  <pre><code>for (i in 2:length(variable1) { ... }
  </code></pre>
  
  <p>The problem arises when you try to assign values like such:</p>
  
  <pre><code>variable2 &lt;- 1:length(variable1)
  for (i in 2:length(variable1) {
      variable2[i] &lt;- sample(variable1, 1)  # silly example; ignore it content-wise
  }
  </code></pre>
  
  <p>Now, no matter if you would have a smaller vector initialized, it will often be problematic with the obtained variable2, since it has a "1" in the first position. What is the easiest way to deal with a for loop when you want to start from a higher number? And is there a better way than having to use <code>i &lt;- i+i</code>, since we then have to also let it run from 1 to length(variable1) minus 1. Am I missing an easy solution? </p>
  
  <p>While I am at it, what is the best way to predefine a variable before a <code>for</code> loop?
  I usually use <code>variable2 &lt;- 1:length(variable1)</code>, when I know variable2 should be a variable  with the length of <code>variable1</code>. Is this the fastest way to allocate memory?</p>
  
  <p>EDIT: I kind of realized just now that it might be easiest to do:</p>
  
  <pre><code>variable2 &lt;- 2:length(variable1)
  for (i in 2:length(variable1) {
      variable2[i-1]
  }
  </code></pre>
  
  <p>But I am still open for better suggestions.</p>