I would like to replace certain words with certain replacements. I made a for loop but only the last replacement takes place.
library(dplyr)
library(stringr)
text <- data.frame(
tekstid = c(1,2),
example = c("here are some examples", "and questions i ask")
)
wordmatch <- data.frame(
word = c("examples", "questions"),
replacement = c("example", "question"))
for(i in 1:nrow(wordmatch)) {
text_output <- text %>%
str_replace_all(example, fixed(wordmatch$word[i]), wordmatch$replacement[i])
return(text_output)
}
print(text_output)
The output is: "c(\"here are some examples\", \"and question i ask\")"
questions correctly turned into question but examples should have turned into example