How to create sequences in R

03.22.2021

R provides us with some simple ways to create a sequence of numbers, as this is a common need in mathematics. In this article, we will look at three ways to create sequences in R.

The first way we can create sequences is using the n:m expression. This will create a sequence from the number n to m. Let's see an example.

1:4
// 1, 2, 3, 4

The second way to create a sequence is to use the seq function. The sequences has three paramters you can use: from, to, and by.

seq(from = 1, to = 7, by = 2)
# 1, 3, 5, 7

The third way, is using the rep function. This function allows you to create sequences that repeat the same number. You will often create sequences of 0's and 1's in mathematics.

rep(1, times = 4)
# 1, 1, 1, 1