seq() Function in R: How to Generate Sequence of Numbers in R

The seq() is a standard general with a default method to generate the sequence of numbers.

seq() Function in R

To generate a sequence of numbers in R, use the seq() method. The seq() is an inbuilt R generic method that generates the regular sequences.

The seq.int() is a primitive method that can be much faster but has a few restrictions.

The seq_along() and seq_len() methods are extremely fast primitives for two common cases.

Syntax

seq(…)

seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
 length.out = NULL, along.with = NULL, …)

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

seq_along(along.with)

seq_len(length.out)

These are various versions of the seq() function in R.

Parameters

The (…) parameter in the seq(…) method is arguments passed to or from methods.

The from and the end is the starting point and ending point of the sequence.

The by parameter is a number: It is an increment of a sequence.

The along.with parameter takes from the length of this argument.

The length.out parameter desired a length of the sequence. It is a non-negative number, which for seq() and seq.int() will be rounded up if fractional. The length.out  parameter is equally spaced values from “from” to “to”.

Example

s <- seq(0, 5)
print(s)

Output

[1] 0 1 2 3 4 5

In this example, we are generating a sequence of o to 5. Here, we have passed the starting point and ending point in the sequence.

If you pass a single argument, then it is considered an ending point, and the starting point is assumed as 1. So your sequence will be from 1 to the ending point.

s <- seq(5)
print(s)

Output

[1] 1 2 3 4 5

In this example, we passed 5 as an ending point. So the sequence is from 1 to 5.

Passing by parameter to seq()

The by parameter is an integer that indicates the increment of the sequence.

s <- seq(0, 10, by = 2)
print(s)

Output

[1] 0 2 4 6 8 10

In this example, we are passing by as 2. That means it gives us alternate values. If we have started with 1, then it will give us 3, 5, 7, 9. It stays below the ending value of the sequence. The by value is a kind of step value.

Passing length.out parameter in seq()

The length.out parameter is the desired length of the sequence.

s <- seq(1, 10, length.out = 11)
print(s)

Output

[1] 1.0 1.9 2.8 3.7 4.6 5.5 6.4 7.3 8.2 9.1 10.0

Here, the length of the sequence is 11. That means it divides the sequence into 11 elements, and there are exact 11 elements in the 1 to 10 sequence.

Example of seq() with along

for (x in list(NULL, letters[1:6], list(1, pi)))
 cat("x=", deparse(x), "; seq(along = x):", seq(along = x), "\n")

Output

x= NULL ; seq(along = x):
x= c("a", "b", "c", "d", "e", "f") ; seq(along = x): 1 2 3 4 5 6
x= list(1, 3.14159265358979) ; seq(along = x): 1 2

The seq_len() function

The seq_len() method returns an integer vector unless it is a long vector when it will be double.

s <- seq_len(5)
print(s)

Output

[1] 1 2 3 4 5

If you pass 0 to the seq_len() method, then it returns the following output.

seq_len(0)

Output

integer(0)

It is not as same as 1:0 because it returns a different output from the above output.

1:0

Output

[1] 1 0

The seq() function’s main advantage is that it behaves more intuitively when n is zero.

The seq_along() creates a sequence from 1 to the length of the input.

seq_along(month.abb)

Output

[1] 1 2 3 4 5 6 7 8 9 10 11 12

R seq.int() function

R seq.int() creates a sequence from one number to another.

seq.int(-2.75, 2.25)

Output

[1] -2.75 -1.75 -0.75 0.25 1.25 2.25

If you get the sign of the by argument wrong, the seq.int() will throw an error.

tryCatch(
 seq.int(-2.75, 2.25, by = -1),
 error = print
)

Output

<simpleError in seq.int(-1.75, 3.25, by = -1): wrong sign in 'by' argument>

R seq() with for loop

You can use the seq() function with for loop, and it works really well.

for (i in seq_along(month.abb)) {
 print(month.abb[i])
}

Output

[1] "Jan"
[1] "Feb"
[1] "Mar"
[1] "Apr"
[1] "May"
[1] "Jun"
[1] "Jul"
[1] "Aug"
[1] "Sep"
[1] "Oct"
[1] "Nov"
[1] "Dec"

Conclusion

Generating a sequence of numbers is a basic operation in any programming language, and R is no different. Since R language is made for data analysis and complex computations, it provides a seq() function to generate the sequence of numbers.

You need to pass the starting point, ending point, and step to create a sequence.

See also

Sum in R

Mean in R

Mode in R

1 thought on “seq() Function in R: How to Generate Sequence of Numbers in R”

Leave a Comment