r - adist for groups of words -


i have table like:

df<-data.frame(palabra=c('ani', 'anib', 'alop', 'alope','ber', 'beren')) 

and need create distance matrix groups of words, grouped 1st character.

to add:

df$letra<-substring(df$palabra,1,1) 

now need apply adist function each group. put example of adist:

adist(df$palabra, costs=list(insertions=1, deletions=1, substitutions=2)) 

how can create 1 table of distance each group?

a simple combination of lapply , split give want want:

#split used create 2 data frames; 1 group , 1 #for groupb b #lapply apply adist function each of groups lapply(split(df, df$letra), function(x) {   adist(x$palabra, costs=list(insertions=1, deletions=1, substitutions=2)) }) 

output:

$a      [,1] [,2] [,3] [,4] [1,]    0    1    5    6 [2,]    1    0    6    7 [3,]    5    6    0    1 [4,]    6    7    1    0  $b      [,1] [,2] [1,]    0    2 [2,]    2    0 

Comments