A range of numeric values.

number_line(l, r, id = NULL, gid = NULL)

as.number_line(x)

is.number_line(x)

left_point(x)

left_point(x) <- value

right_point(x)

right_point(x) <- value

start_point(x)

start_point(x) <- value

end_point(x)

end_point(x) <- value

number_line_width(x)

reverse_number_line(x, direction = "both")

shift_number_line(x, by = 1)

expand_number_line(x, by = 1, point = "both")

invert_number_line(x, point = "both")

number_line_sequence(
  x,
  by = NULL,
  length.out = 1,
  fill = TRUE,
  simplify = FALSE
)

Arguments

l

[numeric-based]. Left point of the number_line.

r

[numeric-based]. Right point of the number_line. Must be able to be coerced to a numeric object.

id

[integer]. Unique element identifier. Optional.

gid

[integer]. Unique group identifier. Optional.

x

[number_line]

value

[numeric based]

direction

[character]. Type of number_line reverse. Options are; "increasing", "decreasing" or "both" (default).

by

[integer]. Increment or decrement. Passed to seq() in number_line_sequence().

point

[character]. "start", "end", "left" or "right" point.

length.out

[integer]. Number of splits. For example, 1 for two parts or 2 for three parts. Passed to seq().

fill

[logical]. Retain (TRUE) or drop (FALSE) the remainder of an uneven split.

simplify

[logical]. If TRUE, returns a sequence of finite numbers.

Value

number_line

Details

A number_line object represents a range of numbers. It is made up of a start and end point as the lower and upper ends of the range respectively. The location of the start point - left or right, determines whether it is an "increasing" or "decreasing" number_line. This is the direction of the number_line.

reverse_number_line() - reverse the direction of a number_line. A reversed number_line has its left and right points swapped. The direction argument specifies which type of number_line will be reversed. number_line with non-finite start or end points (i.e. NA, NaN and Inf) can't be reversed.

shift_number_line() - Shift a number_line towards the positive or negative end of the number line.

expand_number_line() - Increase or decrease the width of a number_line.

invert_number_line() - Change the left or right points from a negative to positive value or vice versa.

number_line_sequence() - Split a number_line into equal parts (length.out) or by a fixed recurring width (by).

Examples

number_line(-100, 100)
#> [1] "-100 -> 100"

# Also compatible with other numeric based object classes
number_line(as.POSIXct("2019-05-15 13:15:07", tz = "UTC"),
            as.POSIXct("2019-05-15 15:17:10", tz = "UTC"))
#> [1] "2019-05-15 13:15:07 -> 2019-05-15 15:17:10"

# Coerce compatible object classes to `number_line` objects
as.number_line(5.1); as.number_line(as.Date("2019-10-21"))
#> [1] "5.1 == 5.1"
#> [1] "2019-10-21 == 2019-10-21"

# A test for number_line objects
a <- number_line(as.Date("2019-04-25"), as.Date("2019-01-01"))
is.number_line(a)
#> [1] TRUE

# Structure of a number_line object
left_point(a); right_point(a); start_point(a); end_point(a)
#> [1] "2019-04-25"
#> [1] "2019-01-01"
#> [1] "2019-01-01"
#> [1] "2019-04-25"

# Reverse number_line objects
reverse_number_line(number_line(as.Date("2019-04-25"), as.Date("2019-01-01")))
#> [1] "2019-01-01 -> 2019-04-25"
reverse_number_line(number_line(200, -100), "increasing")
#> [1] "200 <- -100"
reverse_number_line(number_line(200, -100), "decreasing")
#> [1] "-100 -> 200"

c <- number_line(5, 6)
# Shift number_line objects towards the positive end of the number line
shift_number_line(x = c(c, c), by = c(2, 3))
#> [1] "7 -> 8" "8 -> 9"
# Shift number_line objects towards the negative end of the number line
shift_number_line(x = c(c, c), by = c(-2, -3))
#> [1] "3 -> 4" "2 -> 3"

# Change the duration, width or length of a number_line object
d <- c(number_line(3, 6), number_line(6, 3))

expand_number_line(d, 2)
#> [1] "1 -> 8" "8 <- 1"
expand_number_line(d, -2)
#> [1] "5 <- 4" "4 -> 5"
expand_number_line(d, c(2,-1))
#> [1] "1 -> 8" "5 <- 4"
expand_number_line(d, 2, "start")
#> [1] "1 -> 6" "6 <- 5"
expand_number_line(d, 2, "end")
#> [1] "3 -> 8" "8 <- 3"

# Invert `number_line` objects
e <- c(number_line(3, 6), number_line(-3, -6), number_line(-3, 6))
e
#> [1] "3 -> 6"   "-3 <- -6" "-3 -> 6" 
invert_number_line(e)
#> [1] "-3 <- -6" "3 -> 6"   "3 <- -6" 
invert_number_line(e, "start")
#> [1] "-3 -> 6" "-3 -> 6" "3 -> 6" 
invert_number_line(e, "end")
#> [1] "3 <- -6"  "3 <- -6"  "-3 <- -6"

# Split number line objects
x <- number_line(Sys.Date() - 5, Sys.Date())
x
#> [1] "2023-11-07 -> 2023-11-12"
number_line_sequence(x, by = 2)
#> [1] "2023-11-07 -> 2023-11-09" "2023-11-09 -> 2023-11-11"
#> [3] "2023-11-11 -> 2023-11-12"
number_line_sequence(x, by = 4)
#> [1] "2023-11-07 -> 2023-11-11" "2023-11-11 -> 2023-11-12"
number_line_sequence(x, by = 4, fill = FALSE)
#> [1] "2023-11-07 -> 2023-11-11"
number_line_sequence(x, length.out = 2)
#> [1] "2023-11-07 -> 2023-11-09" "2023-11-09 -> 2023-11-12"