I have the following data set that describes the aspects of the schedule for a soccer team. DaysBetweenGames describes how many days are between games. HomeAway describes if the team is playing at home (1) or away (0.5).
DaysBetweenGames <- c(1, 2, 4) HomeAway <- c(0.5, 0, 0.5) Schedule <- data.frame(DaysBetweenGames, HomeAway)
The following code is meant to figure out if the team has to travel or not (travel yes = 0.5, travel no = 0). Travel will be described in two main ways:
- the team must have less than or equal to 2 days between games. AND
a) the most recent game is on the away (a road game) OR
b) the current game is away (a road game)
- if the most recent game was at home, and the current game is away (a road game) then this will also be defined as a travel situation
In the following code, you can see that first the most recent game (away = 1, home =0) is calculated successfully. However, there is some issue in the ifelse statement. it successfully calculates definition 1 of travel. However, does not successfully complete definition 2 for some reason.
Any help clarifying this would be amazing. Thank you so much!
Schedule %>% mutate(MostRecent = ifelse(lag(HomeAway, 1) == 0, 1, 0)) %>% mutate(Travel = ifelse((DaysBetweenGames <= 2) & ((MostRecent == 1) | (HomeAway == 0)), 0.5, ifelse((MostRecent =0) & (HomeAway=0), 0.5, 0))) -> Schedule