I have a long character vector (around 1800 elements, each element has one word = replace), which I want to replace with another long character vector (same number of elements, but a tag is added to the previous vector = replacewith). Texts is a readtext dataframe. I need to preserve the texts in the dataframe, as I need their tags to export them to .txt files afterwards.
for(i in 1:length (replace)){
text<-gsub(replace, replacewith, texts, perl=T)
}
My only replaces the first word on the vector (replace) with the second vector (replacewith) - but I want the loop to run over the entire vector.
I realised I have not told R that it should reiterate over replace, but how do I tell it that it should also reiterate over replacewith and not just replace it with the first entry?
for(i in 1:length (replace)){
text<-gsub(i, replacewith, texts, perl=T)
}
I am putting all of my code below to include a reproducible example:
library(writexl)
library(stringr)
library(tidyverse)
library(readtext)
library(tibble)
texts<-readtext("~/Desktop/taskdesc/texts/*.txt", encoding="UTF-8")
tasks<-readtext::readtext("~/Desktop/taskdesc/tasks/*.docx", encoding="UTF-8")
#I am getting rid of the row names and preparing the replace and replacewith vectors
replace<-unlist(replace)
replace<-data.frame(replace)
replace<-replace$replace
replacewith<-paste(replace, "<TASK>")
##replace and replacewith look as follows:
show (replace)
[1] about other school great present people
show (replacewith)
[1] "about <TASK>" "other <TASK>" "school <TASK>" "great <TASK>"
#for loop to replace each of the words from the replace list in the texts with the replacewith
for(i in 1:length (replace)){
text<-gsub(i, replacewith, texts, perl=T)
}