Skip to content

full_seq only checks for tolerance in one direction #657

Description

@ha0ye

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions