What are good examples of when seq_along
will work, but seq
will produce unintended results?
From the documentation of ?seq
we
If the input to seq is length 1 then the outputs between seq
and seq_along
will be different
x <- 5
for(i in seq(x)){
print(x[i])
}
#[1] 5
#[1] NA
#[1] NA
#[1] NA
#[1] NA
for(i in seq_along(x)){
print(x[i])
}
#[1] 5
We also see a difference if the input is a vector of Dates
x <- Sys.Date() + 1:5
seq(x)
#Error in seq.Date(x) : 'from' must be of length 1
seq_along(x)
#[1] 1 2 3 4 5