how to add a character to a string in R -


this question has answer here:

i have this:

text <- "abcdefg" 

and want this:

"abcde.fg" 

how achieve without assigning new string vector text instead changing element of vector itself? finally, randomly insert dot , not dot character element of vector.

we can try sub capture first 5 characters group ((.{5})) followed 1 or more characters in capture group ((.*)) , replace backreference of first group (\\1) followed . followed second backreference (\\2).

sub("(.{5})(.*)", "\\1.\\2", text) #[1] "abcde.fg" 

note: solution direct , doesn't need paste together.


Comments