Here are two example data frames:
df1 <- data.frame(Time1v1 = c(55.25, 59.36, 40.26, 786.008, 980.569, 11.2, 10.11, 23.11),
Time2v1 = c(81, 12, 13, 11.0112, 93.9, 14.8, 15.3, 78.91))
df2 <- data.frame(Time1v2 = c(10.13, 980.659, 14.42, 90.1, 40.3298, 9234, 59.35),
Time2v2 = c(25.1, 88.9, 120, 911, 22.1253, 81, 15.1))
> df1
Time1v1 Time2v1
1 55.250 81.0000
2 59.360 12.0000
3 40.260 13.0000
4 786.008 11.0112
5 980.569 93.9000
6 11.200 14.8000
7 10.110 15.3000
8 23.110 78.9100
> df2
Time1v2 Time2v2
1 10.1300 25.1000
2 980.6590 88.9000
3 14.4200 120.0000
4 90.1000 911.0000
5 40.3298 22.1253
6 9234.0000 81.0000
7 59.3500 15.1000
I want to compare each and every row of df1
with each and every row of df2
. If the difference between Time1
from df1 and df2 is in the range [-0.1,+0.1]
AND difference in Time2
is in the range [-10,+10]
then that particular row from df1 must be removed.
ATTEMPT TO SOLVE
Here's an attempt to solve this. Is there a better way?
df1$remove <- rep("No", nrow(df1))
for(i in 1:nrow(df1)){
for(j in 1:nrow(df2)){
if(data.table::inrange(df1$Time1v1[i], df2$Time1v2[j] - 0.1, df2$Time1v2[j] + 0.1) && data.table::inrange(df1$Time2v1[i], df2$Time2v2[j] - 10, df2$Time2v2[j] + 10)) {df1$remove[i] <- "remove"}
}
}
This gives me:
> df1
Time1v1 Time2v1 remove
1 55.250 81.0000 No
2 59.360 12.0000 remove
3 40.260 13.0000 remove
4 786.008 11.0112 No
5 980.569 93.9000 remove
6 11.200 14.8000 No
7 10.110 15.3000 remove
8 23.110 78.9100 No
EXPECTED FINAL RESULT
And finally the expected output will be:
> df1[which(df1$remove != "remove"),-3]
Time1v1 Time2v1
1 55.250 81.0000
4 786.008 11.0112
6 11.200 14.8000
8 23.110 78.9100
RELATED
All-to-all setdiff on two numeric vectors with a numeric threshold for accepting matches
Here is a manual (declaring the columns by hand) method to do it,
m1 <- outer(df1$Time1v1, df2$Time1v2, `-`)
m2 <- outer(df1$Time2v1, df2$Timev2, `-`)
i1 <- intersect(which(m1 >= -0.1 & m1 <= 0.1, arr.ind = TRUE)[,1],
which(m2 >= -10 & m2 <= 10, arr.ind = TRUE)[,1])
df1[-i1,]
# Time1v1 Time2v1
#1 55.250 81.0000
#4 786.008 11.0112
#6 11.200 14.8000
#8 23.110 78.9100