r - How do I replace exact set of words? -
this question has answer here:
- making gsub replace entire words? 2 answers
i have set of words exclude analysis. example,
trash<- c("de" , "do", "das", ...., "da") # set can n elements
also, have data.frame named matc 2 variables v1 , v2 apply replacements of each word in trash nothing.
when tried using following code:
for(k in 1:length(pr_us)) { matc$v1<- gsub(pr_us[k], "" , matc$v1 ) matc$v2<- gsub(pr_us[k], "" , matc$v2 ) }
the replacement isn't exact. in other words, if matc$v1 "maria da graça madalena", result "maria graça malena" , following result "maria graça madalena". tried this
for(k in 1:length(pr_us)) { matc$v1<- gsub( paste0(pr_us[k], "\bb") , "" , matc$v1 ) matc$v2<- gsub( paste0(pr_us[k], "\bb") , "" , matc$v2 ) }
but, not work.
is there solution avoiding loop? solution apply functions...
since matching word, more reasonable include space before , after trash word. specific example op gives, can be:
gsub("\\s+da\\s+", " ", "maria da graça madalena") [1] "maria graça madalena"
Comments
Post a Comment