Given a string, I need to make many substitutions for different patterns:
subst <- fread(c("
regex ; replacement
abc\\w*\\b ; alphabet
red ; color
\\d+ ; number
")
, sep = ";"
)
> subst
regex replacement
1: abc\\w*\\b alphabet
2: red color
3: \\d+ number
So, for string text <- c( "abc 24 red bcd")
, the expected output would be:
alphabet number color bcd
I tried the follwing code:
mapply(function(x,y) gsub(x, y, text, perl = T)
, subst$regex
, subst$replacement
)
The output I got:
"alphabet 24 red bcd" "abc 24 color bcd" "abc number red bcd"
This code performs each substitution one at a time, and not all at once. What should I do to get the expected result?