Skip to contents

Modified version of base::seq_len() that returns a zero-length integer in case of a zero-length input instead of throwing an error.

Usage

safe_seq_len(n)

Arguments

n

Desired length of the integer sequence.

Value

An integer sequence starting from 1L.

See also

Other statistical computing / numeric functions: round_to(), safe_max(), safe_min(), stat_mode()

Examples

pal::safe_seq_len(5)
#> [1] 1 2 3 4 5

# this function simply returns a zero-length integer for zero-length inputs...
pal::safe_seq_len(NULL)
#> integer(0)
pal::safe_seq_len(integer())
#> integer(0)

# ...while `seq_len()` throws an error
try(seq_len(NULL))
#> Warning: first element used of 'length.out' argument
#> Error in seq_len(NULL) : 
#>   argument must be coercible to non-negative integer
try(seq_len(integer()))
#> Warning: first element used of 'length.out' argument
#> Error in seq_len(integer()) : 
#>   argument must be coercible to non-negative integer