title: Assign and save values based on function output in R
body: <p>I'm stuck with something that should be very easy.</p>
  <p>I have been working with a function that I want to take a vector or single column of data and output a &quot;color&quot; depending on which range the value falls into.</p>
  <pre><code>band_code &lt;- function(x){
    x &lt;- x[1]
    if(x &lt; (-0.25)){color &lt;- &quot;cold&quot;}
    else if(x &gt; (-0.25) &amp;&amp; x &lt; (0.25)){color &lt;- &quot;off&quot;}
    else if(x &gt; (0.25) &amp;&amp; x &lt; (0.75)){color &lt;- &quot;intermediate&quot;}
    else if(x &gt; (0.75) &amp;&amp; x &lt; (1.25)){color &lt;- &quot;on&quot;}
    else if(x &gt; (1.25)){color &lt;- &quot;extra&quot;}
    return(color)
  }
  </code></pre>
  <p>This works well for simple, single tests:</p>
  <pre><code>&gt; band_code(0.76)
  [1] &quot;on&quot;
  </code></pre>
  <p>And this works well, using a test vector:</p>
  <pre><code>test &lt;- -2:2
  
  for (i in test){
    print(i)
    band = band_code(i)
    print(band)
  }
  
  [1] -2
  [1] &quot;cold&quot;
  [1] -1
  [1] &quot;cold&quot;
  [1] 0
  [1] &quot;off&quot;
  [1] 1
  [1] &quot;on&quot;
  [1] 2
  [1] &quot;extra&quot;
  
  </code></pre>
  <p>But when I try to save the output as a column in an object, it fails:</p>
  <pre><code>test &lt;- as.data.frame(test)
  
  for (i in test){
    band = band_code(i)
    test$band &lt;- band
  }
  
  &gt; test
    test band
  1   -2 cold
  2   -1 cold
  3    0 cold
  4    1 cold
  5    2 cold
  </code></pre>
  <p>The &quot;band&quot; values should be as they were before (&quot;cold&quot;, &quot;cold&quot;, &quot;off&quot;, &quot;on&quot;, &quot;extra&quot;), but I guess it is repeating the first &quot;cold&quot; all 5 times.</p>
  <p>What about saving the output to an object causes the malfunction?</p>
  <p>I think it has to do with the first line in the band_code function (x &lt;- x[1]), but after many various attempts I can't get it to work.</p>
  <p>Help, please!</p>