0

This question is related to Replace the string value with value in the find list in R. However my string and replacement vectors have similar characters:

replacement = c("10deg AC45", "10deg TA45", "15deg AC45", "AC45", "TA45")
string = c("10deg - 02 AC45", ""10deg -05 AC45" , "10deg-02 AC45", "10deg-05 TA45", "15deg-10 AC45", "AC45-10", "TA45-1")

Hence trying:

replaced=string
replaced=''


for (i in 1:length(replacement))
{
  replaced[which(grepl(replacement[i],string))]=replacement[i]
}
replaced[is.na(replaced)]=''

Results in:

[1] "AC45", "AC45", "AC45" "TA45" "AC45" "AC45" "TA45"

Rather than:

"10deg AC45","10deg AC45", "10deg AC45", "10deg TA45", "15deg AC45", "AC45", "TA45"

Any suggestions? Thanks

  • I assume that the order of `replacement` and `string` is not always the same? Also, can they be of different lengths? – Tom Haddow Jan 11 '19 at 15:21
  • Thanks. Order is definitely not the same. The string has values with or without hyphen and sometimes extra spaces: 10deg-02AC45 10deg -02 AC45 10deg -02AC45 I will edit the question to reflect this. – Gustavo A Escobar-Palafox Jan 11 '19 at 15:27
  • Sorry if this is against SO procedure but might it not be possible to clean `string` rather than going down the replacement route? If the issue is always hyphens and digits like the examples in the comment above it wouldn't be too difficult. In your linked question the task was a lot trickier as `string` and `replacement` were **very** different. – Tom Haddow Jan 11 '19 at 15:31
  • Thanks Tom, I will try just to clean it, deleting everything after hyphens. – Gustavo A Escobar-Palafox Jan 11 '19 at 15:35
  • 1
    Sure, the following 3 lines should do the trick: `string <- gsub(" ", "", string)` `string <- gsub("-\\d{1,2}", "", string)` `string <- gsub("deg", "deg ", string)` – Tom Haddow Jan 11 '19 at 15:38
  • That is spot on. Thanks so much! – Gustavo A Escobar-Palafox Jan 11 '19 at 15:40

0 Answers0