The seq() function comes in handy in R when working with sequences, vectors, loops, etc. In this tutorial, we will show you syntax and then give detailed examples of popular situations that need to use the seq() function.
What is the seq() function in R
The seq() function helps you create a customized sequence number used for columns’ value of data frames, elements of vectors or matrixes, or even in loops.
Syntax:
seq(start, end, by, length.out, along.with)
Arguments:
- start: A number assigned to the start point
- end: A number assigned to the endpoint
- by: A number greater than 1 assigned to step
- length.out: A number assigned to the length of the sequence
- along.with: Two numbers assigned to the length of the sequence
How to use the seq() function?
If you want to use the function, you have to determine 4 parameters: the data frame’s name, that is, the data frame to apply the function, the key and value column’s name, and a range of columns to create the pair of key and value.
Below are some examples of using the seq() function.
Create a sequence with the default step
If you want to create a sequence with steps equal to 1, you only need to specify the start and end points. For example, we will create a sequence from 1 to 9.
Code:
# Create a sequence from 1 to 9 with step = 1 mySeq = seq(1,9) print("My sequence number is:") print(mySeq)
Result:
[1] "My sequence number is:"
[1] 1 2 3 4 5 6 7 8 9
Create a sequence with a step greater than 1
To create a sequence number with the difference between consecutive numbers greater than 1, you must pass the value to the parameter “by”. For example, we will create a sequence of even numbers from 0 to 10.
Code:
# Create a sequence of even numbers evenNumber = seq(0,10, by=2) print("My sequence of even number is:") print(evenNumber)
Result:
[1] "My sequence of even number is:"
[1] 0 2 4 6 8 10
Create a sequence with a fixed length
In some cases, you want to create a sequence with a fixed length; for example, if the length is 6, you must pass 6 as the value of the “length.out” parameter. This way, you only need to determine the start and end points. The function will automatically calculate the step between numbers.
Code:
# Create a sequence of length 6 mySeq = seq(0,20, length.out=6) print("My sequence of length six is:") print(mySeq)
Result:
[1] "My sequence of length six is:"
[1] 0 4 8 12 16 20
Create a sequence with a length from x to y
When you pass two values: x and y, to the along.with parameter, you can create a sequence of length equal to the length from x to y. In this way, you also need to determine the start and endpoint.
Code:
# Create a sequence of length equal to the length from 6 to 10 mySeq = seq(0,20, along.with = 6:10) print("My sequence is:") print(mySeq)
Result:
[1] "My sequence is:"
[1] 0 5 10 15 20
Summary
In summary, by providing values to the function’s parameter, you can use the seq() function to create a sequence number with a fixed step of a fixed length. We hope this article is helpful for you. Thanks for reading!
Maybe you are interested:
- The attach() Function In R: How To Use attach() In R
- lag function in R: How To Use Lag Function In R
- The gather() function in R

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP