I want to iterate over a range of numbers from 1:n
when n
is the length of the vector v
in R. Usually I use the syntax for (i in 1:length(v))
, but this fails if n == 0
.
What is the idiomatic way to do this loop? At the moment, I am doing the following, but it seems a little ugly:
# This is in my standard library rng <- function(n)seq(from=1, to=n, length.out=n)
And yes, I know it better for vectorization, but there are situations when vectorization is impossible or will require so much additional work that reading the code is much more difficult.
source share