Search code examples
rmatchingfuzzy-search

Regmatches over two lists in R


I figured out the positions of approximate machtes in the targetframe with the help of the word.library with the following code:

tragetframe <- data.frame(words= c("Important Words",
                                   "I would also Importante worde of thes substring",
                                   "No mention of this crazy sayingsys"))

word.library <- data.frame(mainword = c("important word",
                                        "crazy sayings"),
                           keyID =c("2000", "3000"))

##find position
find <- function(word.library, tragetframe) {
  aregexec(word.library, tragetframe, max.distance = 0.1)
}

positions <- lapply(word.library[,1], find, tragetframe[,1])

Afterwards I want to extract the matched substrings, but it does not work:

extract <- function(tragetframe, positions ) {
  regmatches(tragetframe, positions)
}
extracted_machtes <- lapply(tragetframe[,1], extract, positions)

Hope someone can help me finding a solution.


Solution

  • mapply(regmatches, tragetframe, positions)
         words             <NA>           
    [1,] "Important Word"  Character,0    
    [2,] "Importante word" Character,0    
    [3,] Character,0       "crazy sayings"
    

    ?mapply runs the function inputted with the first element of the other objects, then the second of each and so on.