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:
- For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using
which
andgrepl
would be best because but I am open to suggestions.) - 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
add a comment |
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:
- For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using
which
andgrepl
would be best because but I am open to suggestions.) - 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
add a comment |
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:
- For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using
which
andgrepl
would be best because but I am open to suggestions.) - 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
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:
- For each "top.name" and "top.ad" column, return the row index where the match came from. (I imagine something using
which
andgrepl
would be best because but I am open to suggestions.) - 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
r dataframe levenshtein-distance grepl which
edited Nov 13 at 20:06
asked Nov 10 at 17:43
jvalenti
134112
134112
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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