title: dcast with data.table in a function
body: <p>I am trying to turn the code below, which already works, into a function. </p>
  
  <p>A similar situation, dcast + DT, has already been disscused <a href="https://stackoverflow.com/questions/31295376/in-r-dcast-in-function-pass-column-names-again">here</a>! But i've wasnt able to solve the problem like that. </p>
  
  <p>What I want to achieve is: </p>
  
  <ul>
  <li>Change only two arguments for a multiple lines of code, and</li>
  <li>Write the objects created by each line in the function to working directory</li>
  </ul>
  
  <p>This is the code that works already: </p>
  
  <pre><code>result1 &lt;- dcast(setDT(data), customer_id ~ paste0("num_of_oranges",period), value.var = "num_of_oranges", sum)
  result2 &lt;- dcast(setDT(data), customer_id ~ paste0("num_of_oranges",period) + paste0("SIGN_",sign), value.var = "num_of_oranges", sum)
  result3 &lt;- dcast(setDT(data), customer_id ~ paste0("num_of_oranges",period) + paste0("SIGN_",sign) + paste0("ORIGIN_",origin), value.var = "num_of_oranges", sum)
  </code></pre>
  
  <p>My attempt towards the function: </p>
  
  <p>create.Feature &lt;- function(col1, stat) {</p>
  
  <pre><code>  test1 &lt;- dcast(df, df[[id]] ~ paste0("col1",df[[period]]), value.var = df[["col1"]], stat)  
   return(test1)
    test2 &lt;- dcast(df, df[[id]] ~ paste0("col1",df[[period]]) + paste0("SIGN",df[[sign]]), value.var = df[["col1"]], stat)
    return(test2)
    test3 &lt;- dcast(df, df[[id]] ~ paste0("col1",df[[period]]) + paste0("SIGN",df[[sign]]) + paste0("ORIGIN",df[[origin]]), value.var = df[["col1"]], stat)
    return(test3)
  </code></pre>
  
  <p>And the call:</p>
  
  <pre><code>test_result &lt;- create.Feature("num_of_oranges", sum)
  </code></pre>
  
  <p>I get the following error: 
      Error in .subset2(x, i, exact = exact) : no such index at level 1 </p>
  
  <p>Anyone?</p>