# rep Function in R (3 Examples)

In this R tutorial you’ll learn how to **replicate elements of a vector or list using the rep function**.

The tutorial will consist of this information:

Let’s just jump right in.

## Definition & Basic R Syntax of rep Function
##

**Definition:** The rep R function replicates elements of vectors and lists.

**Basic R Syntax:** You can find the basic R programming syntax of the rep function below.

rep(x, 2) # Basic R syntax of rep function |

rep(x, 2) # Basic R syntax of rep function

In the following, I’ll show three examples for the application of the rep function in the R programming language. So keep on reading…

## Example Data

Have a look at the following example data:

x <- letters[1:3] # Create example vector x # Print example vector # "a" "b" "c" |

x <- letters[1:3] # Create example vector x # Print example vector # "a" "b" "c"

As you can see based on the previous output of the RStudio console, our example data is a character vector containing the three letters a, b, and c.

## Example 1: rep() Function Using times Argument

In this Example, I’ll show how to replicate a vector using the times argument of the rep function. Have a look at the following R code:

rep(x, times = 3) # Apply rep with times argument # "a" "b" "c" "a" "b" "c" "a" "b" "c" |

rep(x, times = 3) # Apply rep with times argument # "a" "b" "c" "a" "b" "c" "a" "b" "c"

The times argument leads to an output were the whole sequence of our input data is repeats multiple times. We have specified the times argument to be equal to 3 and therefore our vector sequence was repeated three times.

## Example 2: rep() Function Using each Argument

In Example 2, I’ll show how to use the each argument of the rep function:

rep(x, each = 3) # Apply rep with each argument # "a" "a" "a" "b" "b" "b" "c" "c" "c" |

rep(x, each = 3) # Apply rep with each argument # "a" "a" "a" "b" "b" "b" "c" "c" "c"

As you can see based on the previous output of the RStudio console, the each argument leads to an output were each vector (or list) element is repeated several times before the next element is repeated.

## Example 3: rep() Function Using len Argument

Example 3 explains how to apply the len argument of the rep command.

rep(x, len = 5) # Apply rep with len argument # "a" "b" "c" "a" "b" |

rep(x, len = 5) # Apply rep with len argument # "a" "b" "c" "a" "b"

The len argument limits the length of the output and therefore cuts off the repetition of the input data at some point. In this example, we cut off the replication after the fifth element, i.e. the character c was not repeated.

## Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the R codes of this tutorial. You can find the video below.

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you might have a look at the related posts of my website. You can find a selection of tutorials about replication of data below:

- Create Sequence of Repeated Values
- Repeat Rows of Data Frame N Times
- R Functions List (+ Examples)
- The R Programming Language

In this R tutorial you learned how to **apply the rep function**. Let me know in the comments section below, if you have further questions.

**5**/

**5**(

**2**votes )

### Statistics Globe Newsletter