I want to calculate levenshteinDist distance between the rownames and colnames of a matrix using mapply function: Because the volume of may matrix is too big and using a nested loop "for" take a very long time to give me the result.
Here's the old code with nested loop:
mymatrix <- matrix(NA, nrow=ncol(dataframe),ncol=ncol(dataframe),dimnames=list(colnames(dataframe),colnames(dataframe)))
distfunction = function (text1, text2) {return(1 - (levenshteinDist(text1, text2)/max(nchar(text1), nchar(text2))))}
for(i in 1:ncol(mymatrix))
{
for(j in 1:nrow(mymatrix))
mymatrix[i,j]=(distfunction(rownames(mymatrix)[i], colnames(mymatrix)[j]))*100
}
I tried to switch nested loop by mapply:
mapply(distfunction,mymatrix)
It gave me this error:
Error in typeof(str2) : argument "text2" is missing, with no default
I planned to apply the levenshteinDist distance to my matrix and then conclude how to apply myfunction.
Is it possible?
Thank you.