This is one of those occasions when I think a for
loop is justified:
input <- rep("Here are a date, a Date, and a DATE",2)
pat <- c("date", "Date", "DATE")
ret <- c("month", "Month", "MONTH")
for(i in seq_along(pat)) { input <- gsub(pat[i],ret[i],input) }
input
#[1] "Here are a month, a Month, and a MONTH"
#[2] "Here are a month, a Month, and a MONTH"
And an alternative courtesy of @flodel
implementing the same logic as the loop through Reduce
:
Reduce(function(str, args) gsub(args[1], args[2], str),
Map(c, pat, ret), init = input)
For some benchmarking of these options, see @TylerRinker’s answer.