R Insert Character Pattern at Specific Location in String (Example Code)

In this R tutorial you’ll learn how to insert characters at specific locations in a string.

Creation of Example Data

x <- "aaabbb"               # Example string in R
x                           # Display example string in RStudio console
# [1] "aaabbb"

Example: Apply gsub Function to Insert Characters in Between String

gsub('^(.{3})(.*)$',        # Using gsub() function in R
     '\1 Something Else \2',
     x)

Related Tutorials

Please find some related R programming tutorials below:

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

Menu
Top