The **rep()** is a built-in **R** function that repeats elements of an object a specified number of times. The **syntax** is **rep(x, times, each)**, where **x** is the object to be repeated, times is the number of times to repeat the object, and each is an optional argument that specifies the number of times to repeat each element of x (by default, each is set to 1, which means that each element of x is repeated once).

The** rep() function in R** has two faster-simplified versions.

**rep.int()****rep_len()**

**Syntax**

`rep(rv, …)`

**Parameters**

The rep() function takes a maximum of two arguments.

The **rv **is a vector (of any mode, including a list) or a factor.

The **(…) **are further arguments for or from other methods. For example, it can be one of the following.

**times:**It is an integer-valued vector giving the (non-negative) number of times to repeat each item if of length. For example, length(x), or to repeat the whole vector if of length 1.**length.out:**It is a non-negative integer—the desired length of the output vector.**each:**It is a non-negative integer. Each item of x is repeated each time.

**Example 1: How to use the rep() function in R**

`rep(11, 4)`

**Output**

`[1] 11 11 11 11`

In this example, we are repeating vector 11 four times.

To repeat NA values more than once, use the rep() function.

`rep(NA, 5)`

**Output**

`[1] NA NA NA NA NA`

**Example 2: Repeat counting from number to number in R**

Repeat the counting numbers from 1 to 4 three times.

`rep(1:4, 3)`

**Output**

` [1] 1 2 3 4 1 2 3 4 1 2 3 4`

You can see that 1 through 4 are repeated four times.

**Example 3: Repeat vector with an incomplete cycle in R**

To repeat a vector with an incomplete cycle, use the **length.out **argument in the rep() function.

`rep(1:4, 3, length.out=9)`

**Output**

`[1] 1 2 3 4 1 2 3 4 1`

By providing **length.out** argument, you can restrict the length of the output.

So, for example, you can see that the cycle is incomplete because the output should consist of 12 integers, but instead, it contains 9 integers.

**Example 4: Passing each argument to the rep() function**

Each parameter is a non-negative integer. Thus, each element of x is repeated each time.

`rep(1:4, each=2)`

**Output**

`[1] 1 1 2 2 3 3 4 4`

You can see that each element from 1 to 4 is repeated 2 times.

**Example 5: Automated length repetition**

A vector can expand to a biased panel by replacing the length parameter with a vector that defines the number of times each item in the vector will repeat.

`rep(1:4, 1:4)`

**Output**

` [1] 1 2 2 3 3 3 4 4 4 4`

Here, you can see that 1 appears 1 time, 2 two times, 3 three times, and 4 four times.

**Replicating elements**

**Example 1: Replicating a single element**

Use the **rep()** function with a single value as the first argument and the number of times you want to replicate the value as the second to replicate a single element in R.

`rep(c(19), times = 3)`

**Output**

`[1] 19 19 19`

**Example 2: Replicating a vector of elements**

Use the **rep()** function with the vector of elements as the first argument and the number of times you want to replicate the vector as the second to replicate the vector elements in R.

```
data <- c(11, 19, 21)
rep(data, times = 2)
```

**Output**

`[1] 11 19 21 11 19 21`

**Example 3: Replicating elements of a matrix or array**

To **replicate** elements of a **matrix** or **array** in **R**, you can use the **rep()** function with the matrix or array as the first argument and the number of times you need to replicate the matrix or array as the second.

```
mat <- matrix(1:9, 3, 3)
matrep <- rep(mat, times = 2)
print(matrep)
```

**Output**

` [1] 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9`

**Example 4: Replicating elements of a list**

Use the **rep()** **function** to replicate a **list** in **R**.

```
data <- list(netflix = 1:4)
rep(data, 4)
```

**Output**

```
$netflix
[1] 1 2 3 4
$netflix
[1] 1 2 3 4
$netflix
[1] 1 2 3 4
$netflix
[1] 1 2 3 4
```

In the above example, the Netflix list of 1 to 4 has been replicated four times.

**Example 5: Replicating elements of a factor**

The rep() function in R can replicate the factor.

```
result <- factor(LETTERS[1:4])
rep(result, 4)
```

**Output**

```
[1] A B C D A B C D A B C D A B C D
Levels: A B C D
```

**Other rep() functions**

**rep.int() function in R**

The **rep.int()** function returns no attributes (except the class returns a factor). The **rep.int()** is a simple case provided as a separate function, partly for compatibility and speed. The syntax is **rep.int(x, times)**.

`rep.int(1:4, 2)`

**Output**

`[1] 1 2 3 4 1 2 3 4`

**rep_len() function in R**

The **rep_len()** function in **R** is similar to the **rep()** function but is used to repeat elements of a vector, matrix, or array to achieve a specific output length.

The **rep_len()** function takes two arguments: the first is the **vector**, **matrix**, or **array** of elements to be repeated, and the second is the **expected** **length** of the output.

**The syntax is rep_len(x, length.out).**

`rep_len(1:3, 10)`

**Output**

` [1] 1 2 3 1 2 3 1 2 3 1`

In this example, 1 to 3 is repeated until the **length.out **is reached.

**FAQ**

** Can you use the rep() function to repeat elements in a non-uniform manner?**

Yes, the **rep()** function helps repeat elements in a non-uniform manner by determining a vector of the number of times to repeat each element in the **‘times’** argument.

**What happens if the ‘times’ argument is negative?**

If the **“times”** argument is negative, the **rep()** function will return an empty vector.

**Can you use the rep() function to repeat elements in a cyclic manner?**

Yes, the **rep()** function can repeat elements in a cyclic manner by specifying the **‘each’** argument.

**Conclusion**

The **rep()** function in **R** is excellent for replicating the values of a list or vector, and it is also time and memory-effective.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.