full_seq() has a tol parameter that is the tolerance for checking periodicity. However, the implementation of the check, which uses the %% operator only checks for tolerances in one direction:
tidyr::full_seq(c(0, 10, 20), 11, tol = 2)
#> Error: `x` is not a regular sequence.
Created on 2019-06-28 by the reprex package (v0.3.0)
Here's an updated full_seq.numeric() that I think solves the issue, and also pads the range if the last element falls short of being k*period more than the first element:
full_seq.numeric.patched <- function(x, period, tol = 1e-6) {
rng <- range(x, na.rm = TRUE)
if (any((x - rng[1]) %% period > tol &
period - (x - rng[1]) %% period > tol)) {
stop("`x` is not a regular sequence.", call. = FALSE)
}
if (period - ((rng[2] - rng[1]) %% period) <= tol)
rng[2] <- rng[2] + tol
seq(rng[1], rng[2], by = period)
}
full_seq.numeric.patched(c(0, 10, 20), 11, tol = 2)
#> [1] 0 11 22
full_seq.numeric.patched(c(0, 10, 20), 11, tol = 1.8)
#> Error: `x` is not a regular sequence.
full_seq.numeric.patched(c(0, 10, 17), 11, tol = 5)
#> [1] 0 11 22
full_seq.numeric.patched(c(0, 10, 16), 11, tol = 5)
#> [1] 0 11
Created on 2019-06-28 by the reprex package (v0.3.0)
full_seq()has atolparameter that is the tolerance for checking periodicity. However, the implementation of the check, which uses the%%operator only checks for tolerances in one direction:Created on 2019-06-28 by the reprex package (v0.3.0)
Here's an updated
full_seq.numeric()that I think solves the issue, and also pads the range if the last element falls short of being k*period more than the first element:Created on 2019-06-28 by the reprex package (v0.3.0)