R function masking scope conflict between data.table and lubridate -
edit: see simple fix below.
i'm running odd behavior. applying via lapply
multiple functions within j
expression of data.table works when explicitly name library in case 1. in case 2, if don't name library, works first time, if call same line second time, fails error message: error in fun(x[[i]], ...) : not find function "f"
# case 1 unloadnamespace('data.table') unloadnamespace('lubridate') require(data.table) require(lubridate) dt <- 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 <- 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
what's causing strange behavior? there solution other explicit scoping lubridate::function
?
edit:
as per @frank's comment below, problem data.table
interpreting year
, month
referencing columns instead of lubridate
functions. changing column names fixed it:
# case 2 unloadnamespace('data.table') unloadnamespace('lubridate') require(data.table) require(lubridate) dt <- 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
however, per @frankh's comment, it's best explicitly specify lubridate::function
remove doubt on of 2 packages masked other.
Comments
Post a Comment