R - stringdist cost setting error

262 views Asked by At

I have an error when I try to set the operations costs in

stringdist 

Any ideas why ?

library(stringdist)

seq = rbind(
  c('aaa'), 
  c('aba'), 
  c('aab'), 
  c('ccc')
)

This works perfectly (Levensthein distance)

stringdistmatrix(a = seq, b = seq, method = 'lv')

When I want to set the costs (substitution twice the indel)

stringdistmatrix(a = seq, b = seq, method = 'lv', weight = c(1,1,2,0))

I have this error

Error: all(weight > 0) is not TRUE
1

There are 1 answers

1
Jota On BEST ANSWER

See ?stringdistmatrix. Specifically, read what it says by the weights argument. You'll see Weights must be positive and not exceed 1. In this case, the error is telling you that one weight is not positive. You have one of the weights set to 0.

Once you fix that, you'll still get an error because you have another weight that exceeds 1.

stringdistmatrix(a = seq, b = seq, method = 'lv', weight = c(1,1,2,1))
# Error: all(weight <= 1) is not TRUE

So, you have to fix that, too. Keep the weights positive and not above 1.

For example

stringdistmatrix(a = seq, b = seq, method = 'lv', weight = c(1,1,1,1))
stringdistmatrix(a = seq, b = seq, method = 'lv', weight = c(0.1, 0.1, 0.1, 0.1))