title: R function masking scope conflict between data.table and lubridate
body: <p>EDIT: See simple fix below.</p>
  
  <hr>
  
  <p>I'm running into some odd behavior. Applying via <code>lapply</code> multiple functions within the <code>j</code> expression of a data.table works when I explicitly name the library in case 1. In case 2, if I don't name the library, it works the first time, but if I call the same line a second time, it fails with the error message: <code>Error in FUN(X[[i]], ...) : could not find function "f"</code></p>
  
  <pre><code># Case 1
  unloadNamespace('data.table')
  unloadNamespace('lubridate')
  require(data.table)
  require(lubridate)
  dt &lt;- data.table(x=as.POSIXct("2013-01-01 00:53:00", tz="Japan"))
  dt[, c('year','month', 'day') := lapply(c(lubridate::year, lubridate::month, lubridate::day), function(f) f(x) )] # passes
  dt[, c('year','month', 'day') := lapply(c(lubridate::year, lubridate::month, lubridate::day), function(f) f(x) )] # passes
  
  # Case 2
  unloadNamespace('data.table')
  unloadNamespace('lubridate')
  require(data.table)
  require(lubridate)
  dt &lt;- data.table(x=as.POSIXct("2013-01-01 00:53:00", tz="Japan"))
  dt[, c('year','month', 'day') := lapply(c(year, month, day), function(f) f(x) )] # passes
  dt[, c('year','month', 'day') := lapply(c(year, month, day), function(f) f(x) )] # fails
  </code></pre>
  
  <p>What's causing this strange behavior? Is there a solution other than the explicit scoping <code>lubridate::function</code>?</p>
  
  <hr>
  
  <h1>EDIT:</h1>
  
  <p>As per @Frank's comment below, the problem was that <code>data.table</code> was interpreting <code>year</code> and <code>month</code> as referencing its columns instead of the <code>lubridate</code> functions. Changing the column names fixed it:</p>
  
  <pre><code># Case 2
  unloadNamespace('data.table')
  unloadNamespace('lubridate')
  require(data.table)
  require(lubridate)
  dt &lt;- data.table(x=as.POSIXct("2013-01-01 00:53:00", tz="Japan"))
  dt[, c('y1','y2', 'y3') := lapply(c(year, month, day), function(f) f(x) )] # passes
  dt[, c('y1','y2', 'y3') := lapply(c(year, month, day), function(f) f(x) )] # passes
  </code></pre>
  
  <p>However, as per @FrankH's comment, it's probably best to explicitly specify <code>lubridate::function</code> to remove any doubt over which of the two packages masked the other.</p>