1

Say we have to different sets of data like:

A <- (1:20)
B <- (9:18)

Now to output the overlapping number the following can be used:

C <- A[B] 

But what if we want the opposite. The following does not work:

C <- A[!B] 

Why, and how can one output the unique values?

lukeg
  • 1,327
  • 3
  • 10
  • 27
  • 2
    If `A` doesn't start with 1, your code won't give you the expected result. – Roland Sep 17 '15 at 12:45
  • Sure. Work fine! Can you recommend any tutorial where one can learn the base R functions?? – lukeg Sep 17 '15 at 12:45
  • 2
    Here: http://stackoverflow.com/tags/r/info –  Sep 17 '15 at 12:47
  • 2
    https://stat.ethz.ch/R-manual/R-devel/library/base/html/00Index.html But it's usually better to think about how a statistician would call such an operation and then use that as search terms. – Roland Sep 17 '15 at 12:47
  • 2
    `!B` is going to give you a vector of FALSE values. Rather use `-B` which will drop those indices from the A vector – pcantalupo Sep 17 '15 at 12:51

1 Answers1

0

A[B] is just subsetting the vector A by the positions in vector B. If you want to use the actual values to get something like the intersection, you can to use the %in% operator.

This discrepancy is unclear using your example because A happens to have the same positions as it does elements, i.e. 1 is in position 1 and 20 is in position 20. Here is another example that shows this slight nuance:

A = (c(1:5, 8:20))
B= (1:10)
A[B]

A[A %in% B]
A[!(A %in% B)]
dc3
  • 188
  • 1
  • 12