My intention is: (1) to extract multiwords/strings (from data1
), (2) to replace those extracted words by other strings located in another dataset (data2
). To make it clear, the objective is replacing mult1
by mult2
after mining mult1
from data1
.
library(sringi)
library(stringr)
data1 <- data.frame(id=c(1,2,3),
text=c("This is text mining exercise text",
"Text analysis is bit confusing analyssi",
"Hint on this text analysis?"))
data2 <- data.frame(mult1 = c("text","analysis","bit confusing"),
mult2 = c("A; B; C","A; D", "A; B; C; D"))
txt <- subf <- list()
for(i in 1:length(data1$id)){
txt[i] <- str_extract_all(data1$text[i],str_c(data2$mult1,collapse="|")) #this works fine
subf[i] <- str_replace_all(txt[i],data2$mult2[i]) #here is my problem
}
For intance, txt[1]
give:
[1] "text" "text"
The corresponding string for text
is "A; B; C"
in this case.What I'm looking for is the code that can produce an ouput like:
"A; B; C" "A; B; C"
Any help is highly appreciated. Tnx!