seq_along() Function in R: Complete Guide

The seq() function acts like the seq_along() method except when passed a vector of length 1, in which case it works like the seq_len() method.

If the input number to seq() function is length 1 then the outputs between seq() and seq_along() will be different. Let’s deep dive into seq_along() function.

seq_along() function in R

The seq_along() in R is a built-in function that creates a vector that contains a sequence of numbers from 1 to the object’s length. Thus, the seq_along() function creates a sequence the same length of the argument passed, and in the context of a for loop, is used to more easily create the index to iterate over.

The seq_along(n) is equivalent to 1:length(n).




The seq_along() method takes a length from the length of this argument.

Return value

The seq_along() function returns an integer vector unless it is a long vector when it will be double.




[1] 1 2 3 4 5
[1] 1 2 3 4 5

In this example, we are creating vectors using LETTERS. You can see that it returns the indices of the vector elements.

You can also use the c() function to create a Vector.

seq_along(c(11, 21, 19))
seq_along(c(1.1, 2.1, 1.9, 1.8))
seq_along(c(200, 4000))


[1] 1 2 3
[1] 1 2 3 4
[1] 1 2

That’s it for this tutorial.

Leave a Comment