Generating a Sequence in R Using the Function seq()
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.
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:
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.
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
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.
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!
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)
Output:
-5 -4 -3 -2 -1 0 1 2 3 4 5
seq.int(2, 10)
Output:
2 3 4 5 6 7 8 9 10
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