Insert counter after match in row
I am trying to insert the suffix count after each match in a string.
For example: Insert a number after each matching "o" in the following line:
"The Apollo program was conceived early in 1960"
It will look like this:
"The Apo1llo2 pro3gram was co4nceived early in 1960"
I think I should use gsub, perhaps, with perl = TRUE, but I do not know how.
string <- "The Apollo program was conceived early in 1960"
gsub( x = string, pattern = "(o)", replacement = "\\1 $count", perl = TRUE )
source
share