Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 956
Dario Lacan Avatar asked Mar 03 '15 14:03

Dario Lacan


1 Answers

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"
like image 88
Andrie Avatar answered Sep 20 '22 06:09

Andrie