I have two tables. I want to match every value in one table to another, and with a criteria, I want to test whether this value is compatible in the second table. After testing, I would like to export the results into a third table, and I would like to do it with the **`for`** **loop** function in r, like:

```
df1 <- read.table(text = "ID1 Value1
A 1
B 2
C 3", stringsAsFactors = FALSE, header = TRUE)

df2 <-read.table(text = "ID1 Value2
D 1
E 2
F 3", stringsAsFactors = FALSE, header = TRUE)

```

For matching **every value** in `df1` to `df2` I want the criteria to be 

**BOTH**

```
df1$Value1 >= 0.5*df2$Value2

```

**AND** 

```
df1$Value1 <= 2*df2$Value2

```

For example, when matching `A` in `df1` to `D` in `df2`, the criteria is `A>=0.5D` **AND** `A<=2D`, if yes, export the result to a new table.

The algorithm will be in this picture:
[![enter image description here](https://i.stack.imgur.com/THglV.png)](https://i.stack.imgur.com/THglV.png)

The final table will be:

```
ID1 Value1 ID2 Value2
A 1 D 1
A 1 E 2
B 2 D 1
B 2 E 2
B 3 F 3
C 3 E 2
C 3 F 3

```