2016-03-31 16 views

Antwort

3

Hier ist ein Ansatz mit dem stringdist Paket.

# Your data sample, plus a couple of extra rows 
dat = data.frame(x=c(1,'bat','tap','tap','tapes','tapped'), 
       y=c(2,'bad','ta','tape','tapes','tapas')) 

dat 
     x  y 
1  1  2 
2 bat bad 
3 tap ta 
4 tap tape 
5 tapes tapes 
6 tapped tapas 

library(stringdist) 

# Distance methods available in stringdist 
dist.methods = c("osa", "lv", "dl", "hamming", "lcs", "qgram", 
       "cosine", "jaccard", "jw", "soundex") 

# Try all the methods with the sample data 
sapply(dist.methods, function(m) stringdist(dat[,1],dat[,2], method=m)) 
 osa lv dl hamming lcs qgram cosine jaccard   jw soundex 
[1,] 1 1 1  1 2  2 1.0000000 1.0000000 1.00000000  1 
[2,] 1 1 1  1 2  2 0.3333333 0.5000000 0.22222222  0 
[3,] 1 1 1  Inf 1  1 0.1835034 0.3333333 0.11111111  1 
[4,] 1 1 1  Inf 1  1 0.1339746 0.2500000 0.08333333  0 
[5,] 0 0 0  0 0  0 0.0000000 0.0000000 0.00000000  0 
[6,] 3 3 3  Inf 5  5 0.3318469 0.5000000 0.30000000  1 

Oder mit adist, wie @thelatemail vorgeschlagen:

apply(dat, 1, function(d) adist(d[1], d[2])) 
[1] 1 1 1 1 0 3 

adist verwendet die Levenshtein Abstand, äquivalent dem lv Methode oben. Dies ist wahrscheinlich die Methode, die Sie wollen.

Erläuterungen zu den verschiedenen Entfernungsmethoden finden Sie unter this web page.

1

Hier ist der Code, ich denke, das ist, was Sie erwarten.

df 
    one two 
    bat bad 
    tap ta 
    tap tape 

getDiff<-function(dataframe){ 
    result<-" " 
    for(i in 1:nrow(dataframe)) 

    str1<-unlist(strsplit(dataframe[i,"one"],split = "")) 
    str2<-unlist(strsplit(dataframe[i,"two"],split = "")) 
    for(j in 1:length(str1)){ 
     if(j <= length(str2) & str1[j] == str2[j]){ 
     retstr<-str1[(j+1):length(str1)] 
     }else{ 
     break 
     } 
    } 
    result[i]<-paste(retstr,collapse = "") 
    } 
    return(result) 
} 

getDiff(df) 


results: 
"t" "p" "" 

weiß ich nicht, ob diese Standardfunktion gibt es zu tun ... kann dies hilfreich sein wird ...

Verwandte Themen