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" |
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" |
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) } |
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" |
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.
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.
- Capitalize First Letter of Each Word in Character String
- Print Character String to Newline of RStudio Console
- strsplit Function in R
- R Programming Tutorials
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.
Statistics Globe Newsletter
6 Comments. Leave new
Really Useful Thanks
Thanks a lot for the kind words Rco! 🙂
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.
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
Thank you so much!
You are very welcome, glad it helped! 🙂