bug in function bounds()

Issue #17 resolved
Hongyang Li created an issue

If the input is two discontinuous numbers, bounds returns a wrong value. If the input is one number, bounds report an error. For example,

bounds(c(2,5)) start end length [1,] 2 5 4

bounds(c(2)) Error in names(bounds) <- c("start", "end", "length") : 'names' attribute [3] must be the same length as the vector [2]

Comments (5)

  1. Hongyang Li reporter

    My suggestion is change following part

    if (length(nums) <= 2) { bounds <- sort(nums) bounds <- c(bounds, (bounds[2] - bounds[1] + 1)) names(bounds) <- c("start", "end", "length") return(t(as.matrix(bounds))) }

    to

    if (length(nums) == 1) { bounds <- c(nums, nums, 1) names(bounds) <- c("start", "end", "length") return(t(as.matrix(bounds))) }

  2. Lars Skjærven

    Right... In fact, duplicate the first number of 'nums', and you're there:

    Include the following two lines after you check for length==1.

    if (length(nums) == 2) nums <- c(nums[1], nums)

    Not a very elegant solution either.. but it works.

  3. Hongyang Li reporter

    I think there is no need to check for length(nums) == 2. Because bounds() works well when length(nums) >= 2. We only need to check for length(nums) == 1. Thank you, Lars.

  4. Log in to comment