14177

subtracting two data frames

suppose I have two data frames (DF1 & DF2) and both contain the (x,y) coordinates. I would like to extract the pair of (x,y) that is in DF1 but not DF2. Example:

```DF1<-data.frame(x=1:3,y=4:6,t=10:12) DF2<-data.frame(x=3:5,y=6:8,s=1:3) ```

I want to get

```DF_new<-data.frame(x=1:2,y=4:5,t=10:11). ```

What should I do for much larger data sets? Thanks!

For very large data sets you may be interested in `data.table`:

```library(data.table) DF1<-data.frame(x=1:3,y=4:6,t=10:12) DF2<-data.frame(x=3:5,y=6:8,s=1:3) library(data.table) DF1 <- data.table(DF1, key = c("x", "y")) DF2 <- data.table(DF2, key = c("x", "y")) DF1[complete.cases(DF1[DF2])] # maybe you want this? DF2[DF1] DF1[!DF2] # or maybe you want this? DF2[!DF1] ```

Seems like using `merge` is a good candidate here:
```merge(DF1,DF2) x y t s 1 3 6 12 1 ```
```library(tidyverse) DF1<-data.frame(x=1:3,y=4:6,t=10:12) DF2<-data.frame(x=3:5,y=6:8,s=1:3) anti_join(DF1, DF2) #> Joining, by = c("x", "y") #> x y t #> 1 1 4 10 #> 2 2 5 11 ```