title: update data.table subset with function
body: <p>I have a data.table </p>
  
  <pre><code>dt2 &lt;- data.table(urn=1:10,freq=0, freqband="")
  dt2$freqband = NA
  dt2$freq &lt;- 1:7 #does give a warning message
  ##    urn freq freqband
  ## 1:   1    1       NA
  ## 2:   2    2       NA
  ## 3:   3    3       NA
  ## 4:   4    4       NA
  ## 5:   5    5       NA
  ## 6:   6    6       NA
  ## 7:   7    7       NA
  ## 8:   8    1       NA
  ## 9:   9    2       NA
  ##10:  10    3       NA
  </code></pre>
  
  <p>i also have a function that I am wanting to use to group my freq column</p>
  
  <pre><code>fn_GetFrequency &lt;- function(numgifts) {
      if (numgifts &lt;5) return("&lt;5")
      if (numgifts &gt;=5) return("5+")
      return("ERROR")
  }
  </code></pre>
  
  <p>I am wanting to set the freqband column based on this function. In some cases it will be all records, in some cases it will be a subset. My current approach is (for a subset):</p>
  
  <pre><code>dt2[dt2$urn &lt; 9, freqband := fn_GetFrequency(freq)]
  </code></pre>
  
  <p>using this approach I get the warning:</p>
  
  <pre><code>Warning message:
  In if (numgifts &lt; 5) return("&lt;5") :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>then it sets all the records to have  a value of "&lt;5" rather than the correct value. I'm figuring that I need to use some sort of lapply/sapply/etc function, however I still haven't been able to quite grasp how they work in order to use them to solve my problem.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>EDIT: How might you do this if you use a function that requires 2 parameters?</p>
  
  <p>UPDATED: to include the output of dt2 after my attempted update</p>
  
  <pre><code>    urn freq freqband
   1:   1    1       &lt;5
   2:   2    2       &lt;5
   3:   3    3       &lt;5
   4:   4    4       &lt;5
   5:   5    5       &lt;5
   6:   6    6       &lt;5
   7:   7    7       &lt;5
   8:   8    1       &lt;5
   9:   9    2       NA
  10:  10    3       NA
  </code></pre>
  
  <p>UPDATE: I tried this code to and it worked to deliver the desired output, and it allows me to have a function I can call in other places of code too.</p>
  
  <pre><code>dt2[dt2$urn &lt; 9, freqband := sapply(freq, fn_GetFrequency)]
  </code></pre>