-3

I have a list of lists like this:

My sample data

How can I find every number from 1-100 that is not in the list(cluster)?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Xiaoxixi
  • 91
  • 8
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not every helpful – MrFlick Sep 03 '20 at 21:14
  • 1
    specifically, running `dput(cluster)` and pasting the results into your question as a code chunk would work well ... – Ben Bolker Sep 03 '20 at 22:39

2 Answers2

1

I believe something like

cluster <- list(c(30,37,21), c(10,19,20), c(22, 10, 11))
setdiff(1:100, unlist(cluster))

should work. unlist() collapses the list into a single vector of integers; setdiff(x,y) finds all the values in x that are not contained in y.

Slightly less efficiently, but more generally

v <- 1:100
u <- unlist(cluster)
v[!v %in% u]
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

If 1:100 is the complete space for values in the cluster, maybe you can try

(1:100)[-unlist(cluster)]

since the values in the cluster can play as indices as well in your case here.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81