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.


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.

#r programming #function

The rep() function in R - A Brief Reference
1.35 GEEK