I want to make a new column loop as below:
The first column is the index of the household. the second column is the index of person in that household . the third column is the index of trip of each day in each family during a day. the zoneOfHome is the zone of the home of that household. start_zone is a zone that a person start his/her trip from there and end_zone is the zone of a place that a person travel to there. last column is the purpose of each trip.
A loop is a sequence of trips which starts from home and ends at home.
I want a new column 'loop' which determine loop of each trip of household member.
tibble::tribble(
~Household, ~person, ~trip, ~ZoneOfHome, ~start_zone, ~end_zone, ~purpose,
1L, 1L, 1L, 22L, 22L, 13L, "work",
1L, 1L, 2L, 22L, 13L, 22L, "home",
1L, 1L, 3L, 22L, 22L, 34L, "shopping",
1L, 1L, 4L, 22L, 34L, 22L, "home",
1L, 2L, 1L, 22L, 22L, 13L, "work",
1L, 2L, 2L, 22L, 13L, 22L, "home",
2L, 1L, 1L, 15L, 15L, 15L, "work",
2L, 1L, 2L, 15L, 15L, 15L, "home",
2L, 1L, 3L, 15L, 15L, 45L, "shopping",
2L, 1L, 4L, 15L, 45L, 15L, "home",
3L, 1L, 1L, 17L, 6L, 17L, "home",
3L, 1L, 2L, 17L, 17L, 10L, "work",
3L, 1L, 3L, 17L, 10L, 17L, "home"
)
Foe each person a loop is start when start_zone=zone until end_zone=zone and purpose=home:
Household person trip ZoneOfHome start_zone end_zone loop
1 1 1 22 22 13 1
1 1 2 22 13 22 1
1 1 3 22 22 34 2
1 1 4 22 34 22 2
1 2 1 22 22 13 1
1 2 2 22 13 22 1
2 1 1 15 15 15 1
2 1 2 15 15 15 1
2 1 3 15 15 45 2
2 1 4 15 45 15 2
3 1 1 17 6 17 -
3 1 2 17 17 10 1
3 1 3 17 10 17 1