Мы можем использовать gsubfn
library(gsubfn)
gsubfn("([a-z]\\d+)", setNames(as.list(df1$code), df1$string), str1)
#[1] "10; b: 20, 30, 40; c: 50, 60, 70; d: 80, 90, 100, 110"
Для отредактированной версии
gsubfn("(\\w+ ?\\w+?)", setNames(as.list(df2$code), df2$string), str2)
#[1] "a; b: 20, 30, 40; c: 50, 60, 40"
data
str1 <- "a1; b: b1, b2, b3; c: c1, c2, c3; d: d1, d2, d3, d4"
df1 <- structure(list(string = c("a1", "b1", "b2", "b3", "c1", "c2",
"c3", "d1", "d2", "d3", "d4"), code = c(10L, 20L, 30L, 40L, 50L,
60L, 70L, 80L, 90L, 100L, 110L)), class = "data.frame",
row.names = c(NA, -11L))
str2 <- "a; b: peter, joe smith, john smith; c: luke, james, john smith"
df2 <- structure(list(string = c("a", "peter", "joe smith", "john smith",
"luke", "james", "john smith"), code = c(10L, 20L, 30L, 40L,
50L, 60L, 70L)), class = "data.frame", row.names = c(NA, -7L))