Insert Character Pattern at Particular Position of String in R (2 Examples)


On this page, I’ll show how to insert a character pattern at a particular position of a string in the R programming language.

Table of contents:

Let’s dive right in…


Creation of Example Data

The first step is to create some exemplifying data:

my_string <- "abcdefghi"                       # Create example string
my_string                                      # Print example string
# [1] "abcdefghi"

The previous RStudio console output shows that our exemplifying data is a single character string object containing a sequence of alphabetical letters.


Example 1: Insert Character Pattern in String Using gsub() Function

In this example, I’ll explain how to use the gsub function to insert a character pattern in our example string.

Consider the following R code:

my_string_new1 <- gsub("^(.{5})(.*)$",         # Apply gsub
my_string_new1                                 # Print new string
# [1] "abcde_XXX_fghi"

Have a look at the previous output of the RStudio console: We have created a new character string called my_string_new1 that contains the characters of our input data and in between the characters “_XXX_”.

The previously shown R code works fine. However, it might be a bit complicated to run this R code every time we want to put a particular character pattern into a string.

The next example therefore shows how to speed up this process.


Example 2: Insert Character Pattern in String Using Manually Defined Function

The R code below explains how to create a user-defined function that inserts a character pattern in the middle of character strings.

Let’s first create our user-defined function:

fun_insert <- function(x, pos, insert) {       # Create own function
  gsub(paste0("^(.{", pos, "})(.*)$"),
       paste0("\\1", insert, "\\2"),

Next, we can apply our own function:

my_string_new2 <- fun_insert(x = my_string,    # Apply own function
                             pos = 5, 
                             insert = "_YYY_")
my_string_new2                                 # Print new string
# [1] "abcde_YYY_fghi"

As you can see, we have created another character string containing our input characters and the pattern “_YYY_” in the middle of the string.


Video & Further Resources

Do you want to learn more about character string manipulation in R? Then I recommend watching the following video of my YouTube channel. I’m illustrating the R codes of this article in the video.


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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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


Furthermore, you may want to have a look at some of the other tutorials on this homepage.


In summary: In this article you have learned how to insert characters at specific locations in a string in the R programming language. In case you have further comments or questions, please let me know in the comments section.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

6 Comments. Leave new

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.