I am still new to writing R scripts by myself, but this is what I want to do:

DF1:

```
Chr    Start    End    Region
chr6    3324    3360   Region1
chr4    2445    2455   Region2
chr1    1034    1090   Region4

```

DF2 (Reference):

```
Site    Chr     Location    Gene    
Site1   chr4    2447        GeneB   
Site2   chr9    1153        GeneT   
Site3   chr6    3350        GeneM   
Site4   chr1    1034        GeneC   
Site5   chr1    2000        GeneU   
Site6   chr6    3359        GeneF   
Site7   chr7    1158        GeneI   
Site8   chr4    2451        GeneO
Site9   chr6    3367        GeneZ   

```

I want to annotate my DataFrame DF1 based on the reference DF2, with the following criteria.

1) Most importantly, the first rule is that the "Chr" should match with the "Chr" of the reference.

2) If it does match, then the "Location" of the "Site" in the reference DataFrame2 should be between the "Start" and "End" values of DataFrame1.

So my result in DF3 should look like this:

DF3 (Extention of DF1)

```
Chr    Start    End    Region      Site    Gene
chr6    3324    3360   Region1   Site3   GeneM
chr6    3324    3360   Region1   Site6   GeneF
chr4    2445    2455   Region2   Site1   GeneB
chr4    2445    2455   Region2   Site8   GeneO
chr1    1034    1090   Region4   Site4   GeneC

```

So Regions get additional rows depending on the number of sites that they have.

I did something as follows, but did not succeed.

```
check <- match(DF1$Chr, DF2$CHR, nomatch=0);
funtion_try <- for (i in DF2$Site) {
if (check > 0 && DF2$Location => DF1$Start && DF1$MAPINFO =< dmr$End)
DF3 <- data.frame (Chr, Start, End, DMR, Site, Gene);
write.table (DF3, file="DF3.txt", sep="\t") }

```

Any help appreciated. Thanks in advance!