seq() Function in R: How to Generate Sequence of Numbers in R

The seq() is a standard general with a default method to generate the sequence of numbers.

seq() Function in R

To generate a sequence of numbers in R, use the seq() method. The seq() is an inbuilt R generic method that generates the regular sequences.

The is a primitive method that can be much faster but has a few restrictions.

The seq_along() and seq_len() methods are extremely fast primitives for two common cases.



seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
 length.out = NULL, along.with = NULL, …), to, by, length.out, along.with, …)



These are various versions of the seq() function in R.


The (…) parameter in the seq(…) method is arguments passed to or from methods.

The from and the end is the starting point and ending point of the sequence.

The by parameter is a number: It is an increment of a sequence.

The along.with parameter takes from the length of this argument.

The length.out parameter desired a length of the sequence. It is a non-negative number, which for seq() and will be rounded up if fractional. The length.out  parameter is equally spaced values from “from” to “to”.


s <- seq(0, 5)


[1] 0 1 2 3 4 5

In this example, we are generating a sequence of o to 5. Here, we have passed the starting point and ending point in the sequence.

If you pass a single argument, then it is considered an ending point, and the starting point is assumed as 1. So your sequence will be from 1 to the ending point.

s <- seq(5)


[1] 1 2 3 4 5

In this example, we passed 5 as an ending point. So the sequence is from 1 to 5.

Passing by parameter to seq()

The by parameter is an integer that indicates the increment of the sequence.

s <- seq(0, 10, by = 2)


[1] 0 2 4 6 8 10

In this example, we are passing by as 2. That means it gives us alternate values. If we have started with 1, then it will give us 3, 5, 7, 9. It stays below the ending value of the sequence. The by value is a kind of step value.

Passing length.out parameter in seq()

The length.out parameter is the desired length of the sequence.

s <- seq(1, 10, length.out = 11)


[1] 1.0 1.9 2.8 3.7 4.6 5.5 6.4 7.3 8.2 9.1 10.0

Here, the length of the sequence is 11. That means it divides the sequence into 11 elements, and there are exact 11 elements in the 1 to 10 sequence.

Example of seq() with along

for (x in list(NULL, letters[1:6], list(1, pi)))
 cat("x=", deparse(x), "; seq(along = x):", seq(along = x), "\n")


x= NULL ; seq(along = x):
x= c("a", "b", "c", "d", "e", "f") ; seq(along = x): 1 2 3 4 5 6
x= list(1, 3.14159265358979) ; seq(along = x): 1 2

The seq_len() function

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

s <- seq_len(5)


[1] 1 2 3 4 5

If you pass 0 to the seq_len() method, then it returns the following output.




It is not as same as 1:0 because it returns a different output from the above output.



[1] 1 0

The seq() function’s main advantage is that it behaves more intuitively when n is zero.

The seq_along() creates a sequence from 1 to the length of the input.



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

R function

R creates a sequence from one number to another., 2.25)


[1] -2.75 -1.75 -0.75 0.25 1.25 2.25

If you get the sign of the by argument wrong, the will throw an error.

tryCatch(, 2.25, by = -1),
 error = print


<simpleError in, 3.25, by = -1): wrong sign in 'by' argument>

R seq() with for loop

You can use the seq() function with for loop, and it works really well.

for (i in seq_along( {


[1] "Jan"
[1] "Feb"
[1] "Mar"
[1] "Apr"
[1] "May"
[1] "Jun"
[1] "Jul"
[1] "Aug"
[1] "Sep"
[1] "Oct"
[1] "Nov"
[1] "Dec"


Generating a sequence of numbers is a basic operation in any programming language, and R is no different. Since R language is made for data analysis and complex computations, it provides a seq() function to generate the sequence of numbers.

You need to pass the starting point, ending point, and step to create a sequence.

See also

Sum in R

Mean in R

Mode in R

1 thought on “seq() Function in R: How to Generate Sequence of Numbers in R”

Leave a Comment