Sarah
Sarah

Reputation: 1

How to see the difference in two strings

I'm trying to find the difference between two columns in a CSV file, which I named Test.

File example

I'd like to add a new column called 'Results' that contains the difference between Events_1 & Events_2. If there is no difference the Results can be blank. This is a basic example, for what I'm trying to accomplish, the real list contains hundreds of events in both columns.

Upvotes: 0

Views: 39

Answers (1)

r2evans
r2evans

Reputation: 160447

Not tested with your data, but

vec2 <- c("hello,goodbye","hello,goodbye")
vec1 <- c("hello","hello,goodbye")
Map(setdiff, strsplit(vec2, "[,\\s]+"), strsplit(vec1, "[,\\s]+"))
# [[1]]
# [1] "goodbye"
# [[2]]
# character(0)

If you need them to be comma-delimited strings, then

mapply(function(a,b) paste(setdiff(a,b), collapse=","), strsplit(vec2, "[,\\s]+"), strsplit(vec1, "[,\\s]+"))
# [1] "goodbye" ""       

Upvotes: 1

Related Questions