I am not familiar with regexes at all and would like to match and replace patterns in R.
I would like to replace the template #1 , #2 in the vector: original = c("#1", "#2", "#10", "#11") with each vector value vec = c(1,2) .
As a result, I am looking for the following vector: c("1", "2", "#10", "#11") I'm not sure how to do this. I tried:
for(i in 1:2) { pattern = paste("#", i, sep = "") original = gsub(pattern, vec[i], original, fixed = TRUE) }
but I get:
#> original #[1] "1" "2" "10" "11"
instead: "1" "2" "#10" "#11"
I would be grateful for any help I can get! Thanks!
Mayou source share