title: R: Defining a function in R including a for loop and if condition
body: <p>let's consider for example the following vector in R: <code>x &lt;- 2*c(1:100)</code>.</p>
  
  <p>Now I want to define a stepwise function which should attain the value <code>x[1] = 2</code> for x if <code>x &gt;= 0 &amp; x &lt; 1</code>´, the <code>x[2] = 4</code> for x if <code>x &gt;= 1 &amp; x &lt; 2</code>´....</p>
  
  <pre><code>I tried to do this with the following code:
  
  y &lt;- 2*c(1:100)
  
  f &lt;- function(x){for(i in 1:100){if((x &gt;= i-1) &amp; (x &lt; i)){y[i]}
                                  }
                  }      
  </code></pre>
  
  <p>This didn't work - what is wrong with my code? </p>
  
  <p>Thanks in advance.</p>