0

I have a group of values called "estados" (the States from USA), and I'm creating a selection of them which I'll call "pena_si" (each number is a State)

pena_si<-estados[c(10,14,11,27,41,36,16,42,51,34,45,28,17,25,18,46,29,43,3,37,4,44,19,26,2,48,31,9,5)]

So, now I'd like to create a selection of the values that aren't in "pena_si" but nothing I try works, so I'd like to know how would you do it. I've tried things like:

estados[estados!==pena_si]

But, as I said, it doesn't work.

paulaberna
  • 15
  • 4
  • 1
    Try `estados[!estados %in% pena_si]` or `setdiff(estados, pena_si)` if all are `unique` – akrun Feb 21 '20 at 18:15
  • Does this answer your question? [Opposite of %in%](https://stackoverflow.com/questions/5831794/opposite-of-in) – camille Feb 21 '20 at 18:44
  • 1
    `estados != pena_si` would be testing that these two vectors aren't equal to each other (`!==` isn't R syntax, works in javascript though), whereas you want to test that one doesn't contain the other – camille Feb 21 '20 at 18:45

2 Answers2

1

Another solution, beside the two proposed by @akrun, is by using -which:

estados[-which(estados %in% pena_si)]
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
1

We can use %in% and negate (!)

estados[!estados %in% pena_si]

Or with setdiff if the values are unique

setdiff(estados, pena_si)
akrun
  • 874,273
  • 37
  • 540
  • 662