While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Generating a sequence in R using the function seq() is vital and has many uses in data analysis. You can generate a particular general sequence from specifying beginning and end numbers as well. In this tutorial, we are going to discuss, how we can use the seq() function to generate the sequences.

Let’s dive !!!

**Seq():** The seq function in R can generate the general or regular sequences from the given inputs.

```
seq(from, to, by, length.out, along.with)
```

Where:

**From**= beginning number of the sequence.**To**= Terminating the number of the sequence.**by**= It is the increment of the given sequence. It is calculated as ((to-from) /(length.out-1)).**length.out**= Decides the total length of the sequence**along.with**= Outputs a sequence of the same length as the input vector.

Well, I know you are super excited to generate a sequence using seq() in R. Without much delay, let’s see how it works.

In this sample, the first number represents ‘from’ and last number represents ‘to’ arguments.

**Serial Numbers:**

```
seq(from=1,to=10)
```

**Output**:

**1 2 3 4 5 6 7 8 9 10**

**Decimal Numbers:**

```
seq(1.0,10.0)
```

**Output**:

**1 2 3 4 5 6 7 8 9 10**

**Negative Numbers:**

```
seq(-1,-10)
```

**Output**:

**-1 -2 -3 -4 -5 -6 -7 -8 -9 -10**

In this section, along with from and to arguments, we are using ‘by’ argument as well.

The **by argument** will increment the given number in the sequence as shown below.

Here, I am illustrating the sample using the keywords as well for the proper view.

```
seq(from=1,to=10,by=2)
```

**The** **Output**:

**1 3 5 7 9**

In the above output, you can observe that the argument ‘by’ increments the sequence by 2 i.e. The beginning number of the sequence 1 gets incremented by 2 each time till the sequence ends at 10.

```
seq(from=3,to=30,by=3)
```

**Result**:

**3 6 9 12 15 18 21 24 27 30**

You can also do this without keywords if you know the syntax well. You will get the same output without keywords. But it’s always recommended to use the keywords for proper documentation and readability.

```
seq(3,30,3)
```

**The Result**:

**3 6 9 12 15 18 21 24 27 30**

**Length.out** is the argument that decides the total length of the sequence.

Let’s see how it works with some illustrations:

```
seq.int(from=3,to=30,length.out=10)
```

**Output**:

**3 6 9 12 15 18 21 24 27 30**

As you can observe in the above output, the length.out argument will structures the sequence with the specified length.

Let’s use this argument to generate a negative sequence.

```
seq(from=-3,to=-30,length.out= 10)
```

**Output =**

**-3 -6 -9 -12 -15 -18 -21 -24 -27 -30**

**Along.with** argument takes an input vector and outputs a new sequence of the same length as the input vector within the specified range of numbers.

Don’t worry about the above lines too much. I will illustrate this with simple examples.

```
y<-c(5,10,15,20)
seq(1,25,along.with = y)
```

**Output**:

**1 9 17 25**

```
df<-c(-1,-5,-10,-2,-4)
seq(-5,10,along.with = df)
```

**Output**:

**-5.00 -1.25 2.50 6.25 10.00**

As the headline says, you can use seq() functions with some arguments with ease. Yes, you heard it right!.

If you wonder, how you can pass the arguments to seq() directly, don’t worry. Follow the below illustration to understand this easily.

```
seq_len(5)
```

**Output =**

**1 2 3 4 5**

```
seq_len(10)
```

**Output =**

**1 2 3 4 5 6 7 8 9 10**

```
seq_len(-10)
```

**Output =**

Error in seq_len(-10):

argument must be coercible to non-negative integer

```
seq.int(-5,5)
```

**-5 -4 -3 -2 -1 0 1 2 3 4 5**

```
seq.int(2,10)
```

**2 3 4 5 6 7 8 9 10**

The seq() function in R is a valuable addition to the list of functions present in R. Using the function, you can generate the regular sequences by passing various arguments as well.

This article is concentrated on the seq() function and it’s various arguments which are illustrated in the above sections. Hope you got some good understating on this topic. **Happy sequencing!!!**

**More Study:** R documentation

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.