I have two dataframes with different number of rows, simple example below:
df= data.frame(Xmin=c(10,15),Xmax=c(20,20),Ymin=c(10,20),Ymax=c(20,25),ID=c(1,2))
df
Xmin Xmax Ymin Ymax ID
1 10 20 10 20 1
2 15 20 20 25 2
df2=data.frame(Xmin=c(13,15,17),Xmax=c(17,17,19),Ymin=c(12,21,20),Ymax=c(18,25,22),ID=c(NA,NA,NA))
df2
Xmin Xmax Ymin Ymax ID
1 13 17 12 18 NA
2 15 17 21 25 NA
3 17 19 20 22 NA
I want to replace the column ID of df2 with the corresponding value in ID column of df for the rows matching the condition.
df2$Xmin >= df$Xmin & df2$Xmax<=df$Xmax & df2$Ymin >= df$Ymin & df2$Ymax<=df$Ymax
The desired output is
Xmin Xmax Ymin Ymax ID
1 13 17 12 18 1
2 15 17 21 25 2
3 17 19 20 22 2
Is there a simple way to achieve this?