ifelse to calculate travel schedule for soccer team issues

Hello everyone,

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:

  1. 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)
  2. 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

'''

is the source of the problem; the first element of a vector that lag is applied to will have a return value of NA.

1 Like

Thank you for your feedback. I am aware of this issue but am not too worried about it at all. however, I do not believe that this is the source of my issues related to the second Travel definition and why it is not working.

I have found the issue in the code. In the Ifelse statement, = must be replaced by ==

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.