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" |
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) |
gsub('^(.{3})(.*)$', # Using gsub() function in R '\1 Something Else \2', x)
Related Tutorials
Please find some related R programming tutorials below: