Match large number of slightly varying restaurant names in "data" vector to appropriate "match" vector:
The stringdistmatrix
function in stringdist
package is great, but runs out of memory for a few 10k x 10k and my data is larger.
Tried as(stringdistmatrix(data, match),'sparseMatrix')
would give hoped for result, but runs out of memory. Hence, I would like to explicitly index pairs using sparseMatrix(i,j,x,dims,dimnames)
with x
calculated by adist()
or similar string distance in hopes that it would fit in memory.
R
data <- c("McDonalds", "MacDonalds", "Mc Donald's", "Wendy's", "Wendys", "Wendy",
"Chipotle", "Chipotle's")
match <- c("McDonalds", "Wendys", "Chipotle")
Trying:
library(Matrix)
library(stringdist)
idx <- expand.grid(a=data,b=match)
idx$row <- match(idx$a,idx$b)
idx$col <- match(idx$b,idx$a)
library(Matrix)
sparseMatrix(i=idx$row,
j=idx$col,
x=ifthen(adist(data,match)<2,1,0),
dims=c(7,3),
dimnames = list(data, match))
Hoped for output to match:
library(stringdist)
as(ifelse(stringdistmatrix(data,match)<2,1,0),'sparseMatrix')