Dario Lacan Dario Lacan - 3 months ago 18
R Question

Extract characters that differ between two strings

I have used

adist
to calculate the number of characters that differ between two strings:

a <- "Happy day"
b <- "Tappy Pay"
adist(a,b) # result 2


Now I would like to extract those character that differ. In my example, I would like to get the string
"Hd"
(or
"TP"
, it doesn't matter).

I tried to look in
adist
,
agrep
and
stringi
but found nothing.

Answer

You can use the following sequence of operations:

  • split the string using strsplit().
  • Use setdiff() to compare the elements
  • Wrap in a reducing function

Try this:

Reduce(setdiff, strsplit(c(a, b), split = ""))
[1] "H" "d"