How to Repeat a Character String N Times in R (2 Examples)

This page explains how to repeat the elements of a character string multiple times in the R programming language.

Construction of Exemplifying Data

x <- "Laaa"                      # Exemplifying character string in R
x                                # Display character string in RStudio console
# [1] "Laaa"

Example 1: Using rep() Function to Create Vector of Character Strings Containing Multiple Repetitions

rep(x, 3)                        # Apply rep() function
# [1] "Laaa" "Laaa" "Laaa"

Example 2: Using strrep() Function to Create Single Character String Containing Multiple Repetitions

strrep(x, 3)                     # Apply strrep() function
# [1] "LaaaLaaaLaaa"

Related Articles

Here, you may find some additional resources that are similar to the topic of this page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed