Frameworker247 - 4 months ago 21

R Question

I have two sets of lists, and need to check each vector in the first list against its corresponding vector in the second list to see what's missing (I'm only interested in what is in the first list that isn't in the second).

Sample data:

`> x <- list(c(100,5,1), c(1,20,5))`

> y <- list(c(1,2,5,10,20,50,100), c(1,20,50,100))

I need to both unlist and use setdiff(), so I'm using a function to do both:

`> lapply(x, function(a,b) setdiff(unlist(a),unlist(b)), y)`

Expected result is nothing for the first set and 5 for the second. Unfortunately, this isn't picking up the 5 that is in x[2] but isn't in y[2]. Instead, this is my result:

`[[1]]`

numeric(0)

[[2]]

numeric(0)

Weirdly, it seems to be matching 5 to 50 (maybe?), because if I change the 5 in x[2] to a digit like 3 that isn't in found in y[2], I get the expected result:

`> x <- list(c(100,5,1), c(1,20,3))`

> lapply(x, function(a,b) setdiff(unlist(a),unlist(b)), y)

[[1]]

numeric(0)

[[2]]

[1] 3

Any idea what's going on? Or is there a better way to do this?