I want to create a tourID that separates trips started from home and ended to home. Following is an example of my data.
| personID | TripID | satartpurpose | endpurpose |
|---|---|---|---|
| 1 | 1 | home | shopping |
| 1 | 2 | shopping | home |
| 2 | 1 | home | work |
| 2 | 2 | work | home |
| 2 | 3 | home | visit friend |
| 2 | 4 | visit friend | home |
So, the final output would be as below:
| personID | TripID | startpurpose | endpurpose | tourID |
|---|---|---|---|---|
| 1 | 1 | home | shopping | 1 |
| 1 | 2 | shopping | home | 1 |
| 2 | 1 | home | work | 2 |
| 2 | 2 | work | home | 2 |
| 2 | 3 | home | visit friend | 3 |
| 2 | 4 | visit friend | home | 3 |
I am new to r and don't know how I can write the code for this. I really appreciate if someone can help me.
Thanks so much, Tina