Repeating the values is pretty easy with the rep() function in R.

Hello folks, this article will talk about the applications of the** rep()** function in R. You may know that, you can also use paste() function to concatenate the values in R.

But, when it comes to the case of repeating values at huge extent then the paste() function fails.

In order to tackle this situation, R offers the function named rep(), which repeats the values based on input arguments / parameters.

Table of Contents

## The rep() function syntax

**rep():** The rep function will repeat the values based on the input parameters in R.

rep(x,times,each,length.out)

Where,

**X =** The input data needs to be repeated.

**Times =**It represents the number of times the values should be repeated.**Each =**It represents that number of times each values should be repeated.**Length.out =**Till what extent the values should be repeated.

rep() has many use cases. This article will be of many examples and it will be fun filled as well. By the end you will get most out of rep() function in R.

## Implementing the rep() function in R

Here, we are going to see a simple example of the rep() function and its working as well.

#repeats 2, 5 times rep(2,5)

Output = 2 2 2 2 2

The above code will repeat the values ‘2’ for ‘5’ times. I hope you understood how this function works.

#repeats 25.5, 6 times rep(25.5,6)

Output = 25.5 25.5 25.5 25.5 25.5 25.5

You can repeated the decimal values as well as shown above.

#repeats -2.5, 8 times rep(-2.5,8)

Output = -2.5 -2.5 -2.5 -2.5 -2.5 -2.5 -2.5 -2.5

Almost all the functions in R deal with negative values and rep() function is not an exemption as well.

You can input a decimal values, positive or negative values and the rep function will return the repeated value according to the input parameters.

### 1. The rep() function in R with “Each” parameter

The “each” argument in the rep() function will define how many times each values should be repeated. There are multiple example below which will clearly shows how this works.

#repeats each value 3 times rep(1:3,each=3)

Output = 1 1 1 2 2 2 3 3 3

Here, you can observe that each number from 1 to 3 is repeated thrice.

#repeats each value 3 times rep(c(1,2,3),each=3)

Output = 1 1 1 2 2 2 3 3 3

In the above code, I have written all the elements which should be repeated thrice as a tuple.

If the numbers are more, you can use 1:20 method instead of writing all the numbers from 1 to 20.

### 2. The rep() function in R with “length.out” parameter

In this section, we are going to see how the length.out parameter will work with rep() function.

We will understand this argument with the help of two examples.

#repeats each value 5 times rep(1:5,times = 5)

Output = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

In the above sample, the numbers from 1 to 5 were repeated 5 times. The total count of the numbers is 25 right?

Well, look at this example below.

##repeats each value 5 times upto 10 values rep(1:5,times = 5,length.out = 10)

1 2 3 4 5 1 2 3 4 5

It is very clear that, the total count is limited to 10 irrespective of repeat times i.e. ‘5’.

So, the length.out function will determine the total length of the output disregarding the number of times they need to be repeated. Let’s take a sample that has strings instead of numbers.

#repeats each value 5 times rep(c("Jhon","Rahul","gaga"),times=5)

Output = "Jhon" "Rahul" "gaga" "Jhon" "Rahul" "gaga" "Jhon" "Rahul" "gaga" "Jhon" "Rahul" "gaga" "Jhon" "Rahul" "gaga"

All the names were repeated by 5 times.

#repeats up to 8 values rep(c("Jhon","Rahul","gaga"),times=5,length.out = 8)

Output = "Jhon" "Rahul" "gaga" "Jhon" "Rahul" "gaga" "Jhon" "Rahul"

Fantastic! The total count of repeated values were reduced to 8 by the argument and the 3rd time, “gaga” wasn’t printed. That’s length.out argument for you.

Same way, you can also elongate the values using length.out.

#repeats up to 20 values rep(1:3,length.out = 20)

Output = 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2

Length.out parameter will determine length of the repeated values. It can able to reduce as well as elongate the values as shown in the above samples.

### 3. Repeating individual values using the rep() function

Till now, we have repeated all the values present in the input. But do you know that you can repeat each values specifically?

Let’s see how it works!

#repeats individual values rep(c(2,5),times = c(5,2))

Output = 2 2 2 2 2 5 5

yay! you can see the above sample where the ‘2’ is repeated 5 times and the ‘5’ is repeated 2 times.

rep(c("R","Programming"),times = c(2,4))

"R" "R" "Programming" "Programming" "Programming" "Programming"

Again the same sample, but this time with strings.

## Wrapping up

Well, the rep() function in R is a very useful function when it comes to repeating the values. The function has numerous applications in R programming due to its diversity in terms of arguments.

You can repeat the values in multiple ways and what else one can expect apart from this. I would like to mention one thing that kindly pay attention while passing the arguments.

And I will be most happiest person if you are able to pull off something out of this article. I hope things were pretty much clear at your end. Don’t hesitate to hit the comment box for any queries.

That’s all for now…

**More study: **R documentation