假设我有两个数据框(DF1 和 DF2)并且都包含(x,y)坐标。我想提取 DF1 但不是 DF2 中的 (x,y) 对。例子:
DF1<-data.frame(x=1:3,y=4:6,t=10:12)
DF2<-data.frame(x=3:5,y=6:8,s=1:3)
我想得到
DF_new<-data.frame(x=1:2,y=4:5,t=10:11).
对于更大的数据集,我应该怎么做?谢谢!
似乎 usingmerge
在这里是一个不错的选择:
merge(DF1,DF2)
x y t s
1 3 6 12 1
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]
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