# Generating a sequence in R using seq() function

Published on August 3, 2022

Prajwal CN

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 !!!

## Let’s start with the syntax

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.

## Generating a sequence in R

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

### 1. Seq() function with argument ‘by’

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

### 2. Seq() function with argument ‘length.out’

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

### 3. Seq() function with argument ‘along.with’

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

### 4. Direct argument passing with seq() function

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

## Wrapping up

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Prajwal CN

author

#### Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

### Try DigitalOcean for free

Click below to sign up and get \$200 of credit to try our products over 60 days!

Sign up

### Popular Topics

##### Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

## Featured on Community

### Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

### Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

### Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

## Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more