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
                       "\\1_XXX_\\2",
                       my_string)
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"),
       x)
}

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.

 

 

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.


11 Comments. Leave new

  • Really Useful Thanks

    Reply
  • REGEX is so darned hard Joachim. How does one ever learn it? I wanted to split a string at the first ( Easy? Not for me. After a few hours of frustration, I gave up and went to Excel’s power query tool. I may not live long enough to learn regex. Thank you for the videos and blog posts, you have remarkable content.

    Reply
    • Hey Robert,

      I definitely understand your frustration. I also find the handling of regular expressions very hard, especially if special characters are involved.

      Usually, I just google my specific problem and get the correct specification from there.

      Regards,
      Joachim

      Reply
  • Thank you so much!

    Reply
  • Works basically fine, but not with strings into which you want to insert a character after pos 255.

    Reply
  • Just left a to-be-moderated comment that it doesn’t work with inserting characters in a longer string after pos 255. I could fix this by using `perl = T` in the gsub-expression. Dunno whether the length of groups has been reduced somewhere since the code here seems to have been used for quite a while already and is the most-used answer on different Q&A platforms.

    Reply
    • Hello Fabian,

      It is interesting that it doesn’t work for the positions after 255. Below I created a sample to show that it works in my case.

      # Create a string longer than 255 characters
      my_string <- paste(rep("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10), collapse = "")
       
      # Apply gsub to insert pattern after the 255th position
      my_string_new1 <- gsub("^(.{255})(.*)$", "\\1_XXX_\\2", my_string)
       
      # Print the result
      print(my_string_new1)

      One thing that comes to my mind is that your string might not be as long as you thought. Please use the nchar() function to check its length.

      Regards,
      Cansu

      Reply
  • Hi Cansu,

    Thanks for getting back to me. You are right your code snippet works, but if you change `255` to `256` it’ll stop working; at least for me.

    It works again if you turn on PERL-compatibility.
    My understanding is that non-PERL regexps in R only support group capturing until 255 characters (which is somewhat odd), but I haven’t experimented with this further.

    PS: Your WordFence plugin stops me from including any output regarding the error message because it considers it unsafe 🙂 But I assume you can easily reproduce this on your end. My R version 4.2.3; just in case this plays a role in this “bug”.

    Reply

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.

Top