0 votes
1 view
in R Programming by (5k points)

What are good examples of when seq_along will work, but seq will produce unintended results?

From the documentation of ?seq we have:

Note that it dispatches on the class of the first argument irrespective of argument names. This can have unintended consequences if it is called with just one argument intending this to be taken as along.with: it is much better to use seq_along in that case.

1 Answer

0 votes
by (23.6k points)

The seq function works same like the seq_along() function except when it is passed a vector of length 1, in which case it works like seq_length.

For example:

a <- c(4, 5, 8)

b <- c(5, 5,2,4)

c <- 15

> seq_along(a)

[1] 1 2 3

> seq_along(b)

[1] 1 2 3 4

> seq_along(c)

[1] 1

> seq(a)

[1] 1 2 3

> seq(b)

[1] 1 2 3 4

> seq(c)

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

...