Ops.Date <- function (e1, e2) { if (nargs() == 1) stop("unary ", .Generic, " not defined for Date objects") boolean <- switch(.Generic, "<" = , ">" = , "==" = , "!=" = , "<=" = , ">=" = TRUE, FALSE) if (!boolean) stop(.Generic, " not defined for Date objects") + if (!nchar(e1)) e1 <- as.Date(e1) + if (!nchar(e2)) e2 <- as.Date(e2) NextMethod(.Generic) } adding the above two lines to Ops.Date makes, e.g. as.Date("1999-12-13") == "1999-06-14" behave as "expected". Does it seem like a good idea? (I was inspired by a student's confusion, and by the fact that Ops.factor does a similar thing -- although in this case it seems more sensible to promote the character to a Date rather than demoting the Date to a character (in which case comparisons might not work right?)) Similar questions might apply to Ops.POSIXt ... Ben Bolker
Note that the first argument still cannot be character since Ops.Date won't get dispatched in that case. On 6/24/06, Ben Bolker <bolker at ufl.edu> wrote:> Ops.Date <- function (e1, e2) > { > if (nargs() == 1) > stop("unary ", .Generic, " not defined for Date objects") > boolean <- switch(.Generic, "<" = , ">" = , "==" = , "!=" = , > "<=" = , ">=" = TRUE, FALSE) > if (!boolean) > stop(.Generic, " not defined for Date objects") > + if (!nchar(e1)) e1 <- as.Date(e1) > + if (!nchar(e2)) e2 <- as.Date(e2) > NextMethod(.Generic) > } > > adding the above two lines to Ops.Date makes, e.g. > > as.Date("1999-12-13") == "1999-06-14" > > behave as "expected". > > Does it seem like a good idea? > (I was inspired by a student's confusion, and by > the fact that Ops.factor does a similar thing -- although > in this case it seems more sensible to promote the > character to a Date rather than demoting the Date > to a character (in which case comparisons might not > work right?)) > > Similar questions might apply to Ops.POSIXt ... > > Ben Bolker > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Maybe Matching Threads
- (Date + difftime) and (POSIXt + difftime) does not use date/time arithmetics (PR#14067)
- R-beta: NextMethod(.Generic) bug
- sprintf("%d", integer(0)) aborts
- (PR#7152) Ops.ts returns non-ts object for univariate operations
- Wishlist: In documentation, say that `+.Date`(Date, difftime) should be called directly or remove 'or an object of class "difftime"' (PR#14072)