r - Using lapply with Dates and the minus function -
i have vector of dates , vector of number-of-days.
dates <- seq(sys.date(), sys.date()+10, by='day') number.of.days <- 1:4 i need list entry each number-of-days, each entry in list dates vector minus corresponding number-of-days, ie.,
list(dates-1, dates-2, dates-3, dates-4) the defintion of - (function (e1, e2)  .primitive("-")) indicates first , second arguments e1 , e2, respectively. following should work.
lapply(number.of.days, `-`, e1=dates) but raises error.
error in
-.date(x[[i]], ...) : can subtract "date" objects
furthermore, following does work:
lapply(number.of.days, function(e1, e2) e1 - e2, e1=dates) is feature or bug?
you can use:
lapply(number.of.days, `-.date`, e1=dates) part of problem - primitive doesn't argument matching.  notice how these same:
> `-`(e1=5, e2=3) [1] 2 > `-`(e2=5, e1=3) [1] 2 from r language definition:
this subsection applies closures not primitive functions. latter typically ignore tags , positional matching, pages should consulted exceptions, include log, round, signif, rep , seq.int.
so in case, end using dates second argument - though attempt specify first.  using "date" method -, not primitive, can work.
so technically, behavior seeing feature, or perhaps "documented inconsistency".  part possibly considered bug r multiple dispatch "date" method - despite method not supporting non-date arguments first argument:
> 1:4 - dates  # dispatches `-.date` despite first argument not being date error in `-.date`(1:4, dates) : can subtract "date" objects 
Comments
Post a Comment