safe_seq: Safe Sequence Generation and a Safe Colon Operator

Description Usage Arguments Details Value Author(s) Examples

Description

Generate safe sequences by strictly enforcing either a positive or negative increment

Usage

1
2
3
safe_seq(from = 1L, to = 1L, by = 1L)

from %:% to

Arguments

from

the starting and (maximal) end values of the sequence. Of length 1 unless just from is supplied as an unnamed argument.

to

the starting and (maximal) end values of the sequence. Of length 1 unless just from is supplied as an unnamed argument.

by

number: increment of the sequence.

Details

Creating sequences using either base::seq() function or Colon operator have a notable draw back of incorrectly generating positional indices when a vector is empty. For example, consider x = NULL. Then, the expression 1:length(x) would evaluate to 1:0 which expands to c(1,0). As a result, any Control may inadvertantly trigger an out of bounds error on the initial run.

The safe_seq() and %:% operator seeks to prevent this issue by enforcing either an ascending or descending sequence depending on the by condition. For the %:% operator, note that this is restricted to being always positive.

Value

An integer vector that is either empty or contains a sequence of numbers.

Author(s)

JJB

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Returns a sequence increasing by 1
safe_seq(1, 4)

# Returns an empty vector
safe_seq(4, 1)

# Similar result
4 %:% 1

# Correct result
1 %:% 4

coatless/rops documentation built on May 13, 2019, 8:47 p.m.