I want to replace misspelling for only the misspelled part. Here is example code. The first bit is setting up a reference dataframe with the wrong and correct spelling.
library(stringr)
corrected <- data.frame(stringsAsFactors=FALSE,
Wrong_spell = c("abdmen", "abdomane", "abdome", "abdumen", "abodmen",
"adnomen", "aabdominal", "abddominal"),
Correct_spell = c("abdomen", "abdomen", "abdomen", "abdomen", "abdomen",
"abdomen", "abdominal", "abdominal") )
these are the elements to be corrected
reported <- c("abdmen pain", "abdomane pain", "abdumenXXX pain")
When I run this code I get the resulting 3 elements
regex_pattern <- setNames(corrected$Correct_spell, paste0("\\b", corrected$Wrong_spell, "\\b"))
str_replace_all(reported, regex_pattern)
> str_replace_all(reported, regex_pattern)
[1] "abdomen pain" "abdomen pain" "abdumenXXX pain"
I would like the code to just replace the part that matches the misspelling, so the third element to becomes "abdomenXXX pain". It corrected the first two, but the third element is unchanged. The code only looks at whole words within the element. Not sure it's possible, but if you have any ideas or potential fixes, please point me where I need to look. Any help greatly appreciated. Thanks in advance.