I have two columns of strings in a dataframe, and for each row I want to see the characters which differ.
E.g given
Lines <- "
a b
cat car
dog ding
cow haw"
df <- read.table(text = Lines, header = TRUE, as.is = TRUE)
return
a b diff
cat car t
dog ding o
cow haw co
I have seen
as well as
where a number of neat solutions are returned, which would work for an individual row (first reference), or act row wise but not exactly what I want (second reference).
Ideally I'd like to use something like this:
Reduce(setdiff, strsplit(c(a, b), split = ""))
I tried:
apply(df, function(a,b) Reduce(setdiff, strsplit(c(a, b), split = "")))
but to no avail.
How can this be done?
p.s. I'm particularly keen to do this using dplyr if possible, but only for stylistic reasons