I have two data.frames which are DF1 and DF2.

DF1 is the passenger record and DF2 is the train record.

I just want to find the user who could take only one train.
What I want to is only choose if there is only one train included in the passenger travel time. The train would be arrive at boarding station after the passenger in, and the train would be arrive at alighting station before the passenger out. DF2$BSEC>DF1$BSEC &DF2$ ASEC

If DF2 meet the constraints, then I want to extract it as below, (e.g.,BSTN is boarding station, ASTN is alighting station) 

```
--psuedo code--
DF3<-subset(DF2, BSTN==DF1$BSTN & ASTN==DF1$ASTN & BSEC>DF1$BSEC & ASEC<DF1$ASEC)

if nrow(DF3)==1 then cbind(DF1,DF2)

--my code now--
for(i in 1:nrow(DF1)){
  DF1.1<-DF1[i,]
  DF3<-subset(DF2, BSTN1==DF1.1$BSTN & ASTN1==DF1.1$ASTN & BSEC1>DF1.1$BSEC & ASEC1<DF1.1$ASEC)

if(nrow(DF3)==1)(aa<-bind_cols(DF1.1,DF3))
  temp.res<-bind_rows(temp.res,aa)
  }

```

---

However, it takes too long time to calculate with for loop, since the total data of DF1 is about 8,000,000 rows and DF2 is about 100,000,000.

is there any method to calculate faster with aggregate or count packages (functions)?

The data and result what I want to is,

DF1

```
ID  BSTN     ASTN   BSEC    ASEC
1   2520    2516    200079  21071
2   2520    2516    200079  51071   
3   228     2516    300079  31071   

```

DF2

```
TRNID   BSTN     ASTN   BSEC    ASEC
1       2520    2516    200080  21061   
2       2520    2516    400079  41231   
3       2520    2516    500079  52171   
4       2520    2516    600079  60101
5       228     2516    300100  31061   
6       228     2516    400079  41231   
7       228     2516    500079  52171   
8       228     2516    600079  60101   

```

res

```
ID  BSTN     ASTN   BSEC    ASEC    TRNID   BSTN     ASTN   BSEC    ASEC
1   2520    2516    200079  21071   1       2520    2516    200080  21061
3   228     2516    300079  31071   5       2520    2516    300100  31061

```

THANK YOU!!