Pattern matching and replacement in R

gsub, r, regex

Solution

Another option using `gsubfn`:

library(gsubfn)
gsubfn("^#([1-2])$",  I, original)   ## Function substituting
[1] "1"   "2"   "#10" "#11"

Or if you want to explicitly use the values of your vector , using vec values:

gsubfn("^#[1-2]$",  as.list(setNames(vec,c("#1", "#2"))), original) 

Or formula notation equivalent to function notation:

gsubfn("^#([1-2])$",  ~ x, original)   ## formula substituting

Problem

I am not familiar at all with regular expressions, and would like to do pattern matching and replacement in R. I would like to replace the pattern `#1`, `#2` in the vector: `original = c("#1", "#2", "#10", "#11")` with each value of the vector `vec = c(1,2)`. The result I am looking for is the following vector: `c("1", "2", "#10", "#11")`. I am not sure how to do that. I tried doing: ``` 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 of: `"1" "2" "#10" "#11"`

Original source