1 Goal


The goal of this tutorial is to understand the use of the seq_len function. In addition we will see different ways to declare sequences in R.


2 seq_len: How and why


The seq_len(number) function creates a sequence that starts at 1 and with steps of 1 finishes at the number value. A common use of this function is to create indexes that match the length of a vector in order to make plots.


2.1 seq_len use example


# We create a sequence that starts at 1 and ends at 50
seq_len(50)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
# Let's create a vector of length 12
my_vector <- c(5, 6, 1, 13, 7, 8, 4, 6, 10, 12, 4, 8)
length(my_vector)
## [1] 12
# We can use the length of the vector to create an index
seq_len(length(my_vector))
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
# Now we can use the seq_len function to plot this vector in two dimensions using this index
plot(seq_len(length(my_vector)), my_vector)

There are other ways to create sequences that we are going to show in this tutorial. However the seq_len function is often used because it only takes one parameter and it is very easy to identify in a dense and complex code.

2.2 Summary

At this point the use of seq_len is very clear. It is a function of one parameter that creates a sequence from 1 to this parameter using steps of 1. It’s easy to spot in the code and it’s commonly used to create indexes using a vector. Now feel free to use this function or follow the tutorial to learn different alternatives.


3 Alternative function


# We can in R easily create sequences with steps of 1 using the : command
1:12
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
10:20
##  [1] 10 11 12 13 14 15 16 17 18 19 20
# We can repeat the previous example using this command
plot(1:length(my_vector), my_vector)


4 The seq main function


# Now it's a good time to read the help page of the seq_len function
# ?seq_len

# We see that it descends from a main function called seq

# This function is more general and can take many parameters that make it very useful
# Let's start creating a sequence from 1 to 20
seq(1, 20)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
# We can also use this function in the main example of this tutorial
# However this function takes two parameters and it's not very practical
plot(seq(1,length(my_vector)), my_vector)

# Now let's change the starting point of the sequence
seq(10,20)
##  [1] 10 11 12 13 14 15 16 17 18 19 20
# See that this is equivalent to 
10:20
##  [1] 10 11 12 13 14 15 16 17 18 19 20
# Now we can print the odd numbers between 1 and 100 using the by = parameter
seq(1, 100, by = 2)
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99
# And the even numbers
seq(0, 100, by = 2)
##  [1]   0   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32
## [18]  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66
## [35]  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
# If we don't know the endpoint of the sequence but the number of entries we can use the length.out parameter
# If we print the first 50 odd numbers we repeat the previous example

seq(by = 2, length.out = 50)
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99

4.1 Final example: The multiplication tables


# We can use the power of the seq function to print the multiplication table of any number
# We start at 0 and by the number we want (5 in this case) we print the first 11 (remember the 0 counts) results
seq(from = 0, by = 5, length.out = 11)
##  [1]  0  5 10 15 20 25 30 35 40 45 50
# Feel free to try any number you want. Change the "by =" parameter and read your multiplication table

5 Conclusion


In this tutorial we have learnt the use of the seq_len function. If you made it this far we have also shown you different alternatives and functions that can be very useful to create sequences.