The Wayback Machine - https://web.archive.org/web/20240617225922/https://www.geeksforgeeks.org/create-sequence-of-repeated-values-in-r/
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. 



Similar Reads

Count repeated values in R
In this article, we will learn how we can count the repeated values in R programming language. We will be using the table() function along with which() and length() functions to get the count of repeated values. The table() function in R Language is used to create a categorical representation of data with the variable name and the frequency in the
3 min read
Compute a Sequence of Equally Spaced Round values in R Programming - pretty() Function
pretty() function in R Language is used to decide sequence of equally spaced round values. Syntax: pretty(x, n) Parameters: x: It is defined as vector data n: length of resultant vector Returns: data vector of equal length interval Example 1: C/C++ Code # Create example vector # Apply pretty function to vector x &amp;lt;- 1:50 pretty(x) Output: [1]
2 min read
Create a Random Sequence of Numbers within t-Distribution in R Programming - rt() Function
rt() function in R Language is used to create a random sequence of values from Student t-distribution. Syntax: rt(n, df, ncp) Parameters: n: Number of observations df: Degree of Freedom ncp: Numeric vector of non-centrality parameters. Example 1: # R Program to create random sequence # from t distribution # Calling rt() Function rt(15, 2) rt(15, 1)
1 min read
Create a two-dimensional array of sequence of even integers in R
In this article, we will discuss how to create a 2D array of sequences of even integers in R Programming Language. Method 1 : Using seq() method The array() method can be used to create an array with &gt;=1 dimensions. This method returns an array with the extents specified in the dim attribute. For a two-dimensional array, the dim attribute contai
3 min read
Repeated K-fold Cross Validation in R Programming
Repeated K-fold is the most preferred cross-validation technique for both classification and regression machine learning models. Shuffling and random sampling of the data set multiple times is the core procedure of repeated K-fold algorithm and it results in making a robust model as it covers the maximum training and testing operations. The working
9 min read
Create a list with random values in R
In this article, we are going to see how to create a list with random values in R programming language. The list can store data of multiple data type. A List can store multiple R objects like different types of atomic vectors such as character, numeric, logical. We can create a list using list() function. We need to pass vector(s) as parameters. Sy
3 min read
How to create a matrix with random values in R?
In this article, we will discuss hw to create a matrix with random values in the R programming language. The functions which are used to generate the random values are: rnorm()runif()rexp()rpois()rbinom()sample() We will use all these functions one by one to create the matrix with random values. Method 1: Using rnorm() rnorm() function basically cr
3 min read
R - Create empty vector and append values
In this article, we will discuss how to create an empty vector and add elements into a vector in R Programming Language. An empty vector can be created by simply not passing any value while creating a regular vector using the c() function. Syntax: c() This will return NULL as an output. Example: C/C++ Code # create an empty vector a a=c() # display
4 min read
Create Boxplot of Multiple Column Values using ggplot2 in R
In this article, we will discuss how to create a boxplot of multiple column values using ggplot2 in R Programming Language. A dataframe can be created by containing values organized in the form of rows and columns. The values may belong to different data types. The reshape2 package is used to aggregate data by using the functions, melt and cast. Th
2 min read
Generating sequenced Vectors in R Programming - sequence() Function
sequence() function in R Language is used to create a vector of sequenced elements. It creates vectors with specified length, and specified differences between elements. It is similar to seq() function. Syntax: sequence(x) Parameters: x: Maximum element of vector Example 1: # R program to create sequence of vectors # Calling sequence() function seq
1 min read
Article Tags :