Create Sequence of Repeated Values in R
Last Updated :
17 May, 2021
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
How to Create, Rename, Recode and Merge Variables in R
Variable manipulation is a key part of working with data in the R Programming Language. These actions, whether they involve adding new variables, renaming old ones, recoding them, or merging them together, are critical for every data analysis process. In this article, we'll delve into the intricacie
3 min read
How to Generate a Sequence of Timestamps in R?
Generating a sequence of timestamps in R is a common task in time series analysis, data simulation, and other areas where time-based data is needed. This article will guide you through various methods for generating sequences of timestamps using base R functions and the lubridate package for handlin
4 min read
How to Find Unique Values and Sort Them in R
Finding and Sorting unique values is a common task in R for data cleaning, exploration, and analysis. In this article, we will explore different methods to find unique values and sort them in R Programming Language. Using sort() with unique()In this approach, we use the unique() function to find uni
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 seq
1 min read
Loops in R (for, while, repeat)
Loops are fundamental constructs in programming that allow repetitive execution of code blocks. In R loops are primarily used for iterating over elements of a vector, performing calculations and automating repetitive tasks. In this article we will learn about different types of loops in R. 1. For Lo
6 min read
Concatenate numerical values in a string in R
Concatenating numerical values into a string in R involves converting numeric data to character strings and then combining them using various string manipulation functions. This is a common task in data preprocessing and reporting, where numerical results need to be embedded within textual descripti
2 min read
Ordering Factor Values in R
In this article, we will see how to order factor values in the R programming language. We can order Factor values using the as.ordered() method. It is available in dplyr() package. So we have to load this package. Syntax: library(dplyr)Â Syntax: as.ordered(factor_data) Example 1 : In this example, w
2 min read
How to Create Pivot Tables in R?
In this article, we will discuss how to create the pivot table in the R Programming Language. The Pivot table is one of Microsoft Excel's most powerful features that let us extract the significance from a large and detailed data set. A Pivot Table often shows some statistical value about the dataset
2 min read
Creating a Vector of sequenced elements in R Programming - seq() Function
In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct
2 min read
Generate Data sets of same Random Values in R Programming - set.seed() Function
The set.seed() function in R ensures that random number generation is consistent across different sessions, allowing for identical results each time the code is executed. This is particularly important when sharing code with others or when results need to be verified. Syntax: set.seed(n) Where: n: s
2 min read