Home > Software design >  pmatch() do not recognise partial stringmatching between vectors
pmatch() do not recognise partial stringmatching between vectors

Time:08-16

I have two vectors.

First one is:

x <-
c("    scharfkantig", "t", " aht  36 üz distal", " seit paartagen", 
"36 vipr", " perk ", "üz bilfuird", " ", " knirscht", 
" schiene empohlen", "  meldet ")

Second one is:

 y <-
c("01", "beobachten", "hkp", "hkp", "abform kiefer", "abform bissfix", 
"opt elek abform", "vipr", "perk", "oberfl anästh", "anästh", 
"vest inj", "inj sept blau", "cx pulpennah", "infiltration", 
"injektion", "infil", "l1", "gezeigt zu achten", "putzdruck", 
"mhu", "gezeigt", "pat gezeigt", "pzr", "psi sbi api", "dentalhygiene", 
"duraphat aufge", "empfindliche stelle aufgetragen", "üz", "duraphat")

Now I want the index of the elements of the second vector if they partially match with the first vector. Logically the output should be: (29 8 9) because (üz, vipr, perk) match.

First of all I used this codes:

which(y %in% x)
match(y, x)

But this didn`t work. Now when I try it with:

pmatch(x= y, table = x,  duplicates.ok = TRUE)

I just get

[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA  7 NA

back and I don`t know why. I do everything as it is written in the documentation.

Thank you very much in advance.

CodePudding user response:

Are you looking for grep?

l <- lapply(trimws(y), grep, trimws(x))
which(sapply(l, \(x) length(x) > 0))
# [1]  8  9 29

Simpler:

which(sapply(trimws(y), \(.y) length(grep(.y, x))) > 0)
#vipr perk   üz 
#   8    9   29 

or (basically the same)

which(sapply(trimws(y), \(.y) sum(grepl(.y, x))) > 0)
#vipr perk   üz 
#   8    9   29
  •  Tags:  
  • r
  • Related