0

Instead of matching the whole the below code is also returning the case where there are matching characters in between.

for(i in 1:dim(Step10)[1]) {
x <- agrep(Step10$Supplier.Name[i], Step9$SUPPLIER_NAME,
         ignore.case=TRUE, value=FALSE, fixed = TRUE,
         max.distance = 0.05, useBytes = FALSE)
x <- paste0(x,"")
Step10$Supplier_Name[i] <- x
}

Below is the code

a<- c("LEARNING LLC", "BOC", "NCC","KAMAY")
b<- c("LEARNING LLC","BOCLKYN CENTRE", "YANCO CORP", "SAMAS DBA KAMAY")

a<- as.data.frame(a)
b<- as.data.frame(b)

colnames(a) <- c("names")
colnames(b) <- c("names")

a$names = as.character(a$names)
b$names = as.character(b$names)

a$name_1 <- ""

for(i in 1:dim(a)[1]) {
  x <- agrep(a$names[i],b$names,
         ignore.case=TRUE, value=TRUE,
         max.distance = 0.05, useBytes = TRUE)
  x <- paste0(x,"")
  a$name_1[i] <- x
  }
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Why are you using `agrep` if you want exact match? Also, you need to add word boundaries for exact matches. – Sotos May 28 '19 at 09:09
  • How to reframe the above code to get the match word by word instead of strings in between. – samad raza khan May 28 '19 at 09:14
  • It's better to share a reproducible example, along with expected output. This will make it easier for us to help you. – Sotos May 28 '19 at 09:16
  • If you see the above code we should only get "LEARNING LLC" and "KAMAY" in the output since they are exact word matching. However it is also matching BOC and NCC which in between string matching. – samad raza khan May 28 '19 at 10:18

1 Answers1

0

Since you want an exact match, you must use grep and add word boundaries around your words, i.e.

grep(paste0('\\b', a, '\\b', collapse = '|'), b, value = TRUE)
#[1] "LEARNING LLC"    "SAMAS DBA KAMAY"
Sotos
  • 51,121
  • 6
  • 32
  • 66