File: full_seq.R

package info (click to toggle)
r-cran-ggvis 0.4.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,716 kB
  • sloc: sh: 25; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#' Generate sequence of fixed size intervals covering range.
#'
#' @param range range
#' @param size interval size
#' @param ... other arguments passed on to methods
#' @keywords internal
#' @export
#' @seealso \code{\link[plyr]{round_any}}
fullseq <- function(range, size, ...) UseMethod("fullseq")

#' @export
fullseq.numeric <- function(range, size, ..., pad = FALSE) {
  # if (zero_range(range)) return(range + size * c(-1, 1) / 2)

  x <- seq(
    round_any(range[1], size, floor),
    round_any(range[2], size, ceiling),
    by = size
  )

  if (pad) {
    # Add extra bin on bottom and on top, to guarantee that we cover complete
    # range of data, whether right = T or F
    c(min(x) - size, x, max(x) + size)
  } else {
    x
  }
}

#' @export
fullseq.POSIXt <- function(range, size, ...) {
  seq(range[1], range[2], by = size)
}
#' @export
fullseq.Date <- fullseq.POSIXt

round_any <- function(x, accuracy, f = round) {
  f(x / accuracy) * accuracy
}