Reputation: 1
I have two lists list_a and list_b. The structure of the lists is as follows
> list_b[1]
[[1]]
[1] ORANGE COLUMNS BLACK BLUE TRIGGER
[6] GREEN DRAUGHT
> list_a[1]
[[1]]
[1] RED SPARROW HAWK TRIGGER BLUE
[6] ORANGE COLUMNS TIGER CAMEROON BULLSEYE
> length(list_a)
[1] 1012
> length(list_b)
[1] 1
As we can see, list_b has only 1 list item which has several strings. I want to compare each element of list_b[1] with all the items of list_a i.e compare list_b[1] with list_a[1];list_b[1] with list_a[2];... and so on. The sequence in which the strings appear in list_b[1] and list_a[1:1012] is not important to me. For each match I would like to view the number of elements in list_b[1] that did not match and also the elements that did not match. Hence the comparison between list_b[1] and list_a[1] should indicate that the elements in list_b that did not match are BLACK,GREEN and DRAUGHT and therefore the count of non-matching elements is 3. I tried this using intersect() but was not successful. Please help.
Upvotes: 0
Views: 2948
Reputation: 386
list_b = list(c('o', 'c', 'bk', 'bl', 't', 'g', 'd'))
list_a = list(c('r', 's', 'h', 't', 'bl', 'o', 'c', 'ti', 'ca', 'bu'),
c('r', 's', 'h', 't', 'bl', 'o', 'c', 'ti', 'ca', 'bu'))
lapply(list_a, function(a) {
b = list_b[[1]]
non.matching = setdiff(b, a)
return(c(length(non.matching), non.matching))
})
or
lapply(list_a, function(a) {
b = list_b[[1]]
non.matching = setdiff(b, a)
return(list(num = length(non.matching), elements = non.matching))
})
Upvotes: 2