Dario Lacan Dario Lacan - 1 year ago 75
R Question

Extract characters that differ between two strings

I have used

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
, it doesn't matter).

I tried to look in
but found nothing.

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download