0

To find diff between 2 vectors - one of the answers mentioned was:

x[is.na(match(x,y))]

with x <- c(1,2,3,4); y <- c(2,3,4.

Can someone please explain this answer in detail ?

koekenbakker
  • 3,524
  • 2
  • 21
  • 30
snk
  • 31
  • 3

1 Answers1

0

Just break it down to steps,

match(x,y)           #If no match is found, it returns (by default) NA
#[1] NA  1  2  3

is.na(match(x,y))    #Thus, is.na will create a logical vector for NA values
#[1]  TRUE FALSE FALSE FALSE

x[is.na(match(x,y))]    #Using the above logical vector as index for x
#[1] 1
Sotos
  • 51,121
  • 6
  • 32
  • 66