title: Modify data.table in function and aggregate
body: <p>I'd like to have a function that first modifies the data.table (if certain criterias are fulfilled) and than aggregates some values.</p>
  <pre><code>df.flights &lt;- flights
  setDT(df.flights)
  
  
  aggregate_flights &lt;- function(x, test = FALSE) {
    
    
    if (test == TRUE) {
      df_flights_red &lt;- df.flights[tailnum == &quot;N9EAMQ&quot; | tailnum == &quot;N950UW&quot; | tailnum == &quot;N460WN&quot;]
    } else {
      df_flights_red &lt;- df.flights
    }
    
    y &lt;- df_flights_red[, .(air_time = sum(air_time, na.rm = TRUE),
               distance = sum(distance, na.rm = TRUE)), 
           by = .(month, x)]
    return(y)
  }
  
  agg &lt;- aggregate_flights(df_flights_red[[tailnum]], TRUE)
  
  </code></pre>
  <p>I always get the error message that object &quot;df_flights_red&quot; can't be found. It seems, that my call of the function isn't correct</p>
  <pre><code>agg &lt;- aggregate_flights(df_flights_red[[tailnum]], TRUE)
  </code></pre>
  <p>How do I have to make this call?</p>