Never define sequences with the : operator inside a package, for example 1:n, in particular if you loop over this sequence is some way. If n is 0, 1:n returns the vector c(1, 0), which can have unintended consequences. You may check in other places wheter n is 0, but you may not always catch all such cases. It's better to get into the habit of a safer alternative.
You should use seq_len(n) instead. If n is 0, this just creates an empty vector. So if for example you loop over seq_len(n) and n is 0, you just don't enter the loop.
Never define sequences with the
:
operator inside a package, for example1:n
, in particular if you loop over this sequence is some way. Ifn
is 0,1:n
returns the vectorc(1, 0)
, which can have unintended consequences. You may check in other places whetern
is 0, but you may not always catch all such cases. It's better to get into the habit of a safer alternative.You should use
seq_len(n)
instead. Ifn
is 0, this just creates an empty vector. So if for example you loop overseq_len(n)
andn
is 0, you just don't enter the loop.Instead of
1:length(x)
, you can useseq_along(x)
.