R - return row indexes for text match









up vote
0
down vote

favorite












I am attempting to find the n best matches of some text strings, via levenshtein distance (adist in R). The following example should clarify:



name <- c("holiday inn", "geico", "zgf", "morton phillips")
address <- c("400 lafayette pl tupelo ms", "227 geico plaza chevy chase md",
"811 quincy st washington dc", "1911 1st st rockville md")

source1 <- data.frame(name, address)

name <- c("williams sonoma", "mamas bbq", "davis polk", "hop a long diner","joes crag shack", "mike lowry place", "holiday inn", "zummer")

name2 <- c(NA, NA, NA, NA, NA, NA, "hi express", "zummer gunsul frasca")
address <- c("2 reads way new castle de", "248 w 4th st newark de",
"1100 21st st nw washington dc", "1804 w 5th st wilmington de",
"1208 kenwood parkway holdridge nb", "4203 ocean drive miami fl",
"400 lafayette pl tupelo ms", "811 quincy st washington dc")
source2 <- data.frame(name, name2, address)


The following calculates the edit distance using address and name.



dist.mat.nm <- adist(source1$name, source2$name, partial = T, ignore.case = TRUE)
dist.mat.ad <- adist(source1$address, source2$address, partial = TRUE, ignore.case = TRUE)


The following returns the five best matches, each in a column.



imat <- apply(dist.mat.nm, 1, order)[1:5, ]
top.nm <- data.frame(name = source1$name)
tmp <- apply(imat, 1, function(i) source2$name[i])
colnames(tmp) <- paste("top", 1:5, sep = ".")
top.nm <- cbind(top.nm, tmp)

imat <- apply(dist.mat.ad, 1, order)[1:5, ]
top.ad <- data.frame(address = source1$address)
tmp <- apply(imat, 1, function(i) source2$address[i])
colnames(tmp) <- paste("top", 1:5, sep = ".")
top.ad <- cbind(top.ad, tmp)


What I would like to do is:



  1. For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using which and grepl would be best because but I am open to suggestions.)

  2. Return the value of adist in another column.

The desired result is, for each top.ad, top.nm column, a corresponding index.match column and a distance column containing the value of adist.



For example, the row indexes for top.name.1 are c(7, 6, 4, 1).



Any help would be much appreciated. Thanks.



UPDATE: I found that the following code provides the row index for the first match, but I would like to be able to use vectors for x and y:



find.index <- function(x, y) return(which(grepl(paste(x, collapse = "|"), y, fixed = F)))
vec <- find.index(source1$name, source2$name)


How would I return a whole vector?










share|improve this question



























    up vote
    0
    down vote

    favorite












    I am attempting to find the n best matches of some text strings, via levenshtein distance (adist in R). The following example should clarify:



    name <- c("holiday inn", "geico", "zgf", "morton phillips")
    address <- c("400 lafayette pl tupelo ms", "227 geico plaza chevy chase md",
    "811 quincy st washington dc", "1911 1st st rockville md")

    source1 <- data.frame(name, address)

    name <- c("williams sonoma", "mamas bbq", "davis polk", "hop a long diner","joes crag shack", "mike lowry place", "holiday inn", "zummer")

    name2 <- c(NA, NA, NA, NA, NA, NA, "hi express", "zummer gunsul frasca")
    address <- c("2 reads way new castle de", "248 w 4th st newark de",
    "1100 21st st nw washington dc", "1804 w 5th st wilmington de",
    "1208 kenwood parkway holdridge nb", "4203 ocean drive miami fl",
    "400 lafayette pl tupelo ms", "811 quincy st washington dc")
    source2 <- data.frame(name, name2, address)


    The following calculates the edit distance using address and name.



    dist.mat.nm <- adist(source1$name, source2$name, partial = T, ignore.case = TRUE)
    dist.mat.ad <- adist(source1$address, source2$address, partial = TRUE, ignore.case = TRUE)


    The following returns the five best matches, each in a column.



    imat <- apply(dist.mat.nm, 1, order)[1:5, ]
    top.nm <- data.frame(name = source1$name)
    tmp <- apply(imat, 1, function(i) source2$name[i])
    colnames(tmp) <- paste("top", 1:5, sep = ".")
    top.nm <- cbind(top.nm, tmp)

    imat <- apply(dist.mat.ad, 1, order)[1:5, ]
    top.ad <- data.frame(address = source1$address)
    tmp <- apply(imat, 1, function(i) source2$address[i])
    colnames(tmp) <- paste("top", 1:5, sep = ".")
    top.ad <- cbind(top.ad, tmp)


    What I would like to do is:



    1. For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using which and grepl would be best because but I am open to suggestions.)

    2. Return the value of adist in another column.

    The desired result is, for each top.ad, top.nm column, a corresponding index.match column and a distance column containing the value of adist.



    For example, the row indexes for top.name.1 are c(7, 6, 4, 1).



    Any help would be much appreciated. Thanks.



    UPDATE: I found that the following code provides the row index for the first match, but I would like to be able to use vectors for x and y:



    find.index <- function(x, y) return(which(grepl(paste(x, collapse = "|"), y, fixed = F)))
    vec <- find.index(source1$name, source2$name)


    How would I return a whole vector?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am attempting to find the n best matches of some text strings, via levenshtein distance (adist in R). The following example should clarify:



      name <- c("holiday inn", "geico", "zgf", "morton phillips")
      address <- c("400 lafayette pl tupelo ms", "227 geico plaza chevy chase md",
      "811 quincy st washington dc", "1911 1st st rockville md")

      source1 <- data.frame(name, address)

      name <- c("williams sonoma", "mamas bbq", "davis polk", "hop a long diner","joes crag shack", "mike lowry place", "holiday inn", "zummer")

      name2 <- c(NA, NA, NA, NA, NA, NA, "hi express", "zummer gunsul frasca")
      address <- c("2 reads way new castle de", "248 w 4th st newark de",
      "1100 21st st nw washington dc", "1804 w 5th st wilmington de",
      "1208 kenwood parkway holdridge nb", "4203 ocean drive miami fl",
      "400 lafayette pl tupelo ms", "811 quincy st washington dc")
      source2 <- data.frame(name, name2, address)


      The following calculates the edit distance using address and name.



      dist.mat.nm <- adist(source1$name, source2$name, partial = T, ignore.case = TRUE)
      dist.mat.ad <- adist(source1$address, source2$address, partial = TRUE, ignore.case = TRUE)


      The following returns the five best matches, each in a column.



      imat <- apply(dist.mat.nm, 1, order)[1:5, ]
      top.nm <- data.frame(name = source1$name)
      tmp <- apply(imat, 1, function(i) source2$name[i])
      colnames(tmp) <- paste("top", 1:5, sep = ".")
      top.nm <- cbind(top.nm, tmp)

      imat <- apply(dist.mat.ad, 1, order)[1:5, ]
      top.ad <- data.frame(address = source1$address)
      tmp <- apply(imat, 1, function(i) source2$address[i])
      colnames(tmp) <- paste("top", 1:5, sep = ".")
      top.ad <- cbind(top.ad, tmp)


      What I would like to do is:



      1. For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using which and grepl would be best because but I am open to suggestions.)

      2. Return the value of adist in another column.

      The desired result is, for each top.ad, top.nm column, a corresponding index.match column and a distance column containing the value of adist.



      For example, the row indexes for top.name.1 are c(7, 6, 4, 1).



      Any help would be much appreciated. Thanks.



      UPDATE: I found that the following code provides the row index for the first match, but I would like to be able to use vectors for x and y:



      find.index <- function(x, y) return(which(grepl(paste(x, collapse = "|"), y, fixed = F)))
      vec <- find.index(source1$name, source2$name)


      How would I return a whole vector?










      share|improve this question















      I am attempting to find the n best matches of some text strings, via levenshtein distance (adist in R). The following example should clarify:



      name <- c("holiday inn", "geico", "zgf", "morton phillips")
      address <- c("400 lafayette pl tupelo ms", "227 geico plaza chevy chase md",
      "811 quincy st washington dc", "1911 1st st rockville md")

      source1 <- data.frame(name, address)

      name <- c("williams sonoma", "mamas bbq", "davis polk", "hop a long diner","joes crag shack", "mike lowry place", "holiday inn", "zummer")

      name2 <- c(NA, NA, NA, NA, NA, NA, "hi express", "zummer gunsul frasca")
      address <- c("2 reads way new castle de", "248 w 4th st newark de",
      "1100 21st st nw washington dc", "1804 w 5th st wilmington de",
      "1208 kenwood parkway holdridge nb", "4203 ocean drive miami fl",
      "400 lafayette pl tupelo ms", "811 quincy st washington dc")
      source2 <- data.frame(name, name2, address)


      The following calculates the edit distance using address and name.



      dist.mat.nm <- adist(source1$name, source2$name, partial = T, ignore.case = TRUE)
      dist.mat.ad <- adist(source1$address, source2$address, partial = TRUE, ignore.case = TRUE)


      The following returns the five best matches, each in a column.



      imat <- apply(dist.mat.nm, 1, order)[1:5, ]
      top.nm <- data.frame(name = source1$name)
      tmp <- apply(imat, 1, function(i) source2$name[i])
      colnames(tmp) <- paste("top", 1:5, sep = ".")
      top.nm <- cbind(top.nm, tmp)

      imat <- apply(dist.mat.ad, 1, order)[1:5, ]
      top.ad <- data.frame(address = source1$address)
      tmp <- apply(imat, 1, function(i) source2$address[i])
      colnames(tmp) <- paste("top", 1:5, sep = ".")
      top.ad <- cbind(top.ad, tmp)


      What I would like to do is:



      1. For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using which and grepl would be best because but I am open to suggestions.)

      2. Return the value of adist in another column.

      The desired result is, for each top.ad, top.nm column, a corresponding index.match column and a distance column containing the value of adist.



      For example, the row indexes for top.name.1 are c(7, 6, 4, 1).



      Any help would be much appreciated. Thanks.



      UPDATE: I found that the following code provides the row index for the first match, but I would like to be able to use vectors for x and y:



      find.index <- function(x, y) return(which(grepl(paste(x, collapse = "|"), y, fixed = F)))
      vec <- find.index(source1$name, source2$name)


      How would I return a whole vector?







      r dataframe levenshtein-distance grepl which






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 20:06

























      asked Nov 10 at 17:43









      jvalenti

      134112




      134112



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241712%2fr-return-row-indexes-for-text-match%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241712%2fr-return-row-indexes-for-text-match%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo