Open In App

Create Sequence of Repeated Values in R

Last Updated : 17 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A repeated sequence of numbers is an incremental list of numbers arranged in such a way that the difference in the successive numbers is 1, and each of these numbers is repeated for the specified number of times. R provides us with a variety of methods to generate repeated sequences both using vectors or integers. In this article, we are going to see how to create a sequence of Repeated Values in R programming.

Method 1: Using rep() method

The rep() method of base R is used to generate a replicated sequence from a specified vector, where each element of the vector can be repeated at any number of specified times. This method can take a character, floats, or integers-type input vectors.  

Syntax: rep(seq, each)

Arguments: 

  • seq – The vector to generate a sequence of
  • each – The number of times to repeat each element of the sequence

Example 1: Creating a character sequence of repeated value.

R




# declaring vector
vec <- LETTERS[1 : 4]
 
# replicate each letter in vec 3 times
print ("Replicated Sequence")
rep(vec, each = 3)


Output:

[1] "Replicated Sequence"
[1] "A" "A" "A" "B" "B" "B" "C" "C" "C" "D" "D" "D"

Example 2: Creating an integer sequence of repeated value.

R




# declaring vector
vec <- c(60 : 62)
 
# replicate each integer in vec 3 times
print ("Replicated Sequence")
rep(vec, each = 2)


Output

[1] "Replicated Sequence"
[1] 60 60 61 61 62 62

Method 2: Using gl() method.

The gl() method in base R is used to generate factors in form of pattern specification. The output depends on the number of levels, with each of them being replicated k times until the length is achieved. In case, the length is not specified, the length is taken to be a number of levels * number of replications of each level. The drawback in this approach is that it always generates a sequence ranged between 1 to n. But, in case we need to overrule this default labelling, a customized label vector can be assigned to the output. 

Syntax: gl(n, k, length, labels)

Arguments : 

  • n – the total number of levels.
  • k – number of replications of each level.
  • length (By default : n * k ) – length of the output
  • labels – labels used for the factor levels.

Code:

R




# generate a sequence of 1:6 each
# repeated 4 times
gl(6 , 4 )


Output:

 [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6
Levels: 1 2 3 4 5 6

If the length is greater than n×k, then the same sequence is appended again at the end, till the total length of the vector is achieved. 

R




# generate a sequence of 1:6 each
# repeated 3 times until length=20
gl(6 , 3 , length = 20)


Output:

[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 1 1
Levels: 1 2 3 4 5 6

Explanation: Since, 6×3 = 18, therefore, the same sequence starting from the beginning is appended until length=20 is achieved. Therefore, 1 1 is added at the end. 

Also, customized labels can be assigned to the sequence, as a result of which the actual sequence numerals can be replaced by the label vector’s corresponding letters. To ensure proper mapping, the length of the labels’ vector should be same as the number of factor levels. 

R




# generate a sequence of 1:5
# each repeated 2 times assigned
# labels from 100
seq1 <- gl(5, 2, labels = c(100 : 104))
print("Sequence 1")
print (seq1)
 
# generate a sequence of 1:5
# each repeated 2 times assigned
# labels as letters from a
seq2 <- gl(5, 2, labels = letters[ 1 : 5 ])
print("Sequence 2")
print (seq2)


Output:

[1] "Sequence 1"
[1] 100 100 101 101 102 102 103 103 104 104
Levels: 100 101 102 103 104
[1] "Sequence 2"
[1] a a b b c c d d e e
Levels: a b c d e

Explanation: In sequence 1, each instance of 1 in the original sequence is replaced by 101, 2 is replaced by 102, and so on. Similarly, in sequence 2, each instance of 1 is replaced by ‘a’, 2 by ‘b’, and so on. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads