Skip to content
Prev 155820 / 398503 Next

Truncating dates (and other date-time manipulations)

Thanks for that hint:

floor.Date <- function(date, time) {
  as.Date(cut(date, time))
}

ceiling.Date <- function(date, time) {
  parts <- strsplit(time, " ")[[1]]
  if (length(parts) == 1) {
    mult <- 1
    unit <- time
  } else {
    mult <- as.numeric(parts[[1]])
    unit <- parts[[2]]
  }
  unit <- gsub("s$", "", unit)

  up <- c("day" = 1, "week" = 7, "month" = 31, "year" = 365)
  date <- date + mult * up[unit]

  floor.Date(date, time)
}

which does what I want, I think - accepting all unit specifications
that seq.Date and cut.Date do.  I guess I'll just include these
functions in ggplot2, even though it seems like there should be a more
suitable home for them.
I think you miss the point that this is for ggplot2, so that using
plotting primitives from other packages is rather beside the point.
That's exactly what I'm arguing - I feel like your argument is the
equivalent to saying that we don't need floor and ceiling for numbers
because we have cut.

Hadley

Thread (31 messages)

Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Whit Armstrong Truncating dates (and other date-time manipulations) Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Gabor Grothendieck Truncating dates (and other date-time manipulations) Sep 11 Whit Armstrong Truncating dates (and other date-time manipulations) Sep 11 Avram Aelony database table merging tips with R Sep 11 Aaron Mackey database table merging tips with R Sep 11 Aaron Mackey database table merging tips with R Sep 11 Avram Aelony database table merging tips with R Sep 11 Jeffrey J. Hallman Truncating dates (and other date-time manipulations) Sep 11 Aaron Mackey database table merging tips with R Sep 11 Coey Minear database table merging tips with R Sep 11 Coey Minear database table merging tips with R Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Marc Schwartz Truncating dates (and other date-time manipulations) Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Moshe Olshansky database table merging tips with R Sep 11 Moshe Olshansky database table merging tips with R Sep 11 Gabor Grothendieck Truncating dates (and other date-time manipulations) Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Gabor Grothendieck Truncating dates (and other date-time manipulations) Sep 11 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 11 Gabor Grothendieck Truncating dates (and other date-time manipulations) Sep 11 Thomas Lumley database table merging tips with R Sep 11 Jeffrey J. Hallman Truncating dates (and other date-time manipulations) Sep 12 Jeff Ryan Truncating dates (and other date-time manipulations) Sep 12 Jeff Ryan Truncating dates (and other date-time manipulations) Sep 12 Hadley Wickham Truncating dates (and other date-time manipulations) Sep 12 Jeff Ryan Truncating dates (and other date-time manipulations) Sep 12