Tutorials  /  Python

Generating a sequence in R using seq() function

Ccentron Redaktion · November 2024 ·6 min read ·Python, 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 Start with the Syntax

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

Code
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 learn generating a sequence in R using seq() function. 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:

Code
seq(from=1, to=10)

Output:

Code
1 2 3 4 5 6 7 8 9 10

Decimal Numbers:

Code
seq(1.0, 10.0)

Output:

Code
1 2 3 4 5 6 7 8 9 10

Negative Numbers:

Code
seq(-1, -10)

Output:

Code
-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.

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

The Output:

Code
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.

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

Result:

Code
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.

Code
seq(3, 30, 3)

The Result:

Code
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:

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

Output:

Code
3 6 9 12 15 18 21 24 27 30

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

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

Output:

Code
-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.

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

Output:

Code
1 9 17 25
Code
df <- c(-1, -5, -10, -2, -4)
seq(-5, 10, along.with = df)

Output:

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

Follow the below illustration to understand this easily.

Code
seq_len(5)

Output:

Code
1 2 3 4 5
Code
seq_len(10)

Output:

Code
1 2 3 4 5 6 7 8 9 10
Code
seq_len(-10)

Output:

Code
Error in seq_len(-10):
argument must be coercible to non-negative integer
Code
seq.int(-5, 5)

Output:

Code
-5 -4 -3 -2 -1 0 1 2 3 4 5
Code
seq.int(2, 10)

Output:

Code
2 3 4 5 6 7 8 9 10
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Wrapping Up: Generating a sequence in R using seq() function

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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren