The general issue: floating point arithmetics
As mentioned by RHertel in his comment, this has to do with floating point arithmetics and you can read more about it in the answers to this question. You will find everything you need there and I won't discuss this further, since I have nothing meaningful to add.
The solution to your specific example
Your specific example could be solved by working with whole numbers and only converting to the numbers that you actually want in the end. Also this method has its limitations that I will come back to in the end.
So, I basically start by defining the three vectors and the grid as follows:
vector1 <- seq(from = 8, to = 16, by = 2)
vector2 <- c(seq(from = 8, to = 18, by = 2), 26)
vector3 <- seq(from = 6, to = 12, by = 2)
data <- expand.grid(F1 = vector1, F2 = vector2, F3 = vector3)
In this way, I get numeric values that are 10 times larger than you defined them. But this will be easy to correct at the end by simply dividing by 10. The advantage is that for whole numbers, the comparison works as expected:
data_remove = which(data[,1] - data[,2] > 2)
head(data[data_remove, ])
## F1 F2 F3
## 3 12 8 6
## 4 14 8 6
## 5 16 8 6
## 9 14 10 6
## 10 16 10 6
## 15 16 12 6
You can see that the condition is satisfied in all cases. In particular, the row 113 that you mentioned in your question is not removed this time. To get to the data that you actually wanted, you simply need to divide by 10:
data_new <- data[-data_remove, ]/10
head(data_new)
## F1 F2 F3
## 1 0.8 0.8 0.6
## 2 1.0 0.8 0.6
## 6 0.8 1.0 0.6
## 7 1.0 1.0 0.6
## 8 1.2 1.0 0.6
Limitations of this method
I promised to come back to the limitations of this method. From a mathematical point of view, it always works, as long as you use only rational numbers. For instance,
seq(1/3, 5, by = 1/4)
can be rewritten with whole numbers as
seq(4, 60, by = 3)/12
The factor 12 occurs because 12 is the least common multiple of 3 and 4. However, this sequence can not be rewritten with integers because of the irrational numbers in it:
seq(sqrt(2), 7*sqrt(3), by = pi/5)
There is no factor q
such that q * sqrt(2)
and q * pi/5
are both whole numbers. But you could still solve the issue by rounding the numbers. Rounded to two digits after the comma, the sequence expressed in whole numbers is
seq(141, 1212, by = 63)/100
Another limitation may occur with very large numbers. If you have many relevant digits and therefore need to multiply the sequence by very large numbers, comparison will fail again:
(1e18 + 1) > 1e18
## [1] FALSE