How to Generate a Regular Sequence of Numbers in R

Jesse John Feb 02, 2024
  1. Use the Colon Operator to Generate a Regular Sequence of Numbers in R
  2. Use the seq() Function to Generate a Regular Sequence of Numbers in R
How to Generate a Regular Sequence of Numbers in R

This article will discuss generating a regular sequence of numbers in R using the colon operator, :, and the seq() function.

Use the Colon Operator to Generate a Regular Sequence of Numbers in R

The colon operator uses the format from:to. The sequence will start at from and end at or around to.

It follows the following rules:

  • The sequence will increase by 1 if to is more than from and will decrease by 1 otherwise.
  • The to value is included in the sequence if it has a close to 1 difference from the previous. The documentation states that it can differ from 1 by a numeric fuzz of about 1e-7.

The colon operator allows us to create sequences that differ by 1. Let’s see some examples.

Example code:

# Ascending sequence from 1 to 10.
1:10

# Descending sequence from 9 to 0.
9:0

# Ascending sequence from -5.5 to 5.5.
-5.5:5.5

Output:

> # Ascending sequence from 1 to 10.
> 1:10
 [1]  1  2  3  4  5  6  7  8  9  10
>
> # Descending sequence from 9 to 0.
> 9:0
 [1] 9 8 7 6 5 4 3 2 1 0
>
> # Ascending sequence from -5.5 to 5.5.
> -5.5:5.5
 [1] -5.5 -4.5 -3.5 -2.5 -1.5 -0.5  0.5  1.5  2.5  3.5  4.5  5.5

Use the seq() Function to Generate a Regular Sequence of Numbers in R

The colon operator allowed us to make sequences very quickly. But we could only make sequences in which the numbers differed by values of 1 or -1.

The seq() function gives us much more control. Besides from and to, it has the following arguments:

  • We can change the value between consecutive numbers using the by argument.
  • We can specify how many numbers we want using the length.out or along.with arguments.

In the examples, we will see the following:

  • An increasing sequence in which elements differ by a positive value.
  • A decreasing sequence in which elements differ by a negative value.
  • Sequence of a given length after or before a number, differing by a specified value.
  • Sequence of numbers equally spaced between the start and endpoints.
  • Sequence as long as another object.

Example code:

# Increasing sequence with difference of 5.
seq(from=-10, by=5, to=20)
seq(from=-10, by=5, to=22) # TO value can be approximate.

# Decreasing sequence with difference of -2.
seq(from=10, by=-2, to=2)
seq(from=10, by=-2, to=-10)

# Sequence of 6 numbers after 3 with a difference of 3.
seq(from=3, by=3, length.out=6)

# SEE THE DIFFERENCE BETWEEN THE FOLLOWING TWO.
# Sequence of 5 numbers till 100 with a difference of 10.
seq(to=100, by=10, length.out=5)
# Sequence of 5 numbers till 100 with a difference of -10.
seq(to=100, by=-10, length.out=5)

# Sequence of 6 numbers from 10 to 12.
seq(from=10, to=12, length.out=6)
# Sequence of 10 numbers from 5 to 50.
seq(from=5, to=50, length.out=10)

# Sequence as long as another object.
# First create another object.
vec = rep(c("A", "B", "C"), times=3)
vec # Has 9 elements.
seq(from=100, by=5, along.with=vec)
seq(to=100, by=5, along.with=vec)

Output:

> # Increasing sequence with difference of 5.
> seq(from=-10, by=5, to=20)
[1] -10  -5   0   5  10  15  20
> seq(from=-10, by=5, to=22) # TO value can be approximate.
[1] -10  -5   0   5  10  15  20
>
> # Decreasing sequence with difference of -2.
> seq(from=10, by=-2, to=2)
[1] 10  8  6  4  2
> seq(from=10, by=-2, to=-10)
 [1]  10   8   6   4   2   0  -2  -4  -6  -8 -10
>
> # Sequence of 6 numbers after 3 with difference of 3.
> seq(from=3, by=3, length.out=6)
[1]  3  6  9 12 15 18
>
> # SEE THE DIFFERENCE BETWEEN THE FOLLOWING TWO.
> # Sequence of 5 numbers till 100 with difference of 10.
> seq(to=100, by=10, length.out=5)
[1]  60  70  80  90 100
> # Sequence of 5 numbers till 100 with difference of -10.
> seq(to=100, by=-10, length.out=5)
[1] 140 130 120 110 100
>
> # Sequence of 6 numbers from 10 to 12.
> seq(from=10, to=12, length.out=6)
[1] 10.0 10.4 10.8 11.2 11.6 12.0
> # Sequence of 10 numbers from 5 to 50.
> seq(from=5, to=50, length.out=10)
 [1]  5 10 15 20 25 30 35 40 45 50
>
> # Sequence as long as another object.
> # First create another object.
> vec = rep(c("A", "B", "C"), times=3)
> vec # Has 9 elements.
[1] "A" "B" "C" "A" "B" "C" "A" "B" "C"
> seq(from=100, by=5, along.with=vec)
[1] 100 105 110 115 120 125 130 135 140
> seq(to=100, by=5, along.with=vec)
[1]  60  65  70  75  80  85  90  95 100

Other Uses of the seq() Function in R

We can use the seq() function to make sequences of odd numbers, even numbers, and multiples of a number. The seq() function also helps prevent errors.

For example, see the error messages in the following example when giving arguments that cannot be used.

Example code:

# TO is greater, but BY is negative.
seq(from=1, to=10, by=-1)

# We want a decreasing sequence of 10 numbers with differences of 5 ending at 80.
# The error message tells us that there is a mix-up.
seq(to=80, by=5, length.out=-10)

Output:

> # TO is greater, but BY is negative.
> seq(from=1, to=10, by=-1)
Error in seq.default(from = 1, to = 10, by = -1) :
  wrong sign in 'by' argument
>
> # We want a decreasing sequence of 10 numbers with differences of 5 ending at 80.
> # The error message tells us that there is a mix-up.
> seq(to=80, by=5, length.out=-10)
Error in seq.default(to = 80, by = 5, length.out = -10) :
  'length.out' must be a non-negative number

Other Sequences and Patterns

The seq() function can also generate a sequence of dates.

Moreover, R has many other valuable functions to generate vectors of numbers or letters.

  • The rep() function helps us replicate elements of vectors. These can be numbers or strings.
  • We can generate statistical data that fits a theoretical probability distribution with functions such as rnorm(). Follow the links in its documentation to see the complete list of such functions.

Example code:

# Sequence of dates.
seq(from=as.Date("2022-01-31"), by="day", length.out=5)

# Replicated patterns.
# Whole vector repeated.
rep(c("A", "B", "C"), times=4)
# Each element repeated.
rep(c("A", "B", "C"), each=4)
# Repeat elements and the vector.
rep(c("A", "B", "C"), each=2, times=3)

# Generate 6 random values from a normal distribution with mean=5 and sd=2.
rnorm(6, mean=5, sd=2)

Output:

> # Sequence of dates.
> seq(from=as.Date("2022-01-31"), by="day", length.out=5)
[1] "2022-01-31" "2022-02-01" "2022-02-02" "2022-02-03" "2022-02-04"
>
> # Replicated patterns.
> # Whole vector repeated.
> rep(c("A", "B", "C"), times=4)
 [1] "A" "B" "C" "A" "B" "C" "A" "B" "C" "A" "B" "C"
> # Each element repeated.
> rep(c("A", "B", "C"), each=4)
 [1] "A" "A" "A" "A" "B" "B" "B" "B" "C" "C" "C" "C"
> # Repeat elements and the vector.
> rep(c("A", "B", "C"), each=2, times=3)
 [1] "A" "A" "B" "B" "C" "C" "A" "A" "B" "B" "C" "C" "A" "A" "B" "B" "C" "C"
>
> # Generate 6 random values from a normal distribution with mean=5 and sd=2.
> rnorm(6, mean=5, sd=2)
[1] 4.775072 4.925386 6.758762 3.402821 5.791017 6.864015
Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.