I have data from a sports team tracking wins/losses versus other teams with the following structure:
Game TotalWins TotalLosses Team1Win Team1Loss Team2Win Team2Loss ...
1 1 0 1 NA NA NA
2 1 1 NA NA NA 1
3 2 1 NA NA 1 NA
4 2 2 NA 1 NA NA
5 3 2 NA NA 1 NA
...
I want to create a factor variable that includes the team the game was played against so that the data looks like this:
Game TotalWins TotalLosses Team1Win Team1Loss Team2Win Team2Loss Team
1 1 0 1 NA NA NA Team1
2 1 1 NA NA NA 1 Team2
3 2 1 NA NA 1 NA Team2
4 2 2 NA 1 NA NA Team1
5 3 2 NA NA 1 NA Team2
...
My thinking (NOT workable code) is essentially this:
if (Team1Win == 1 | Team1Loss == 1), Team = "Team1"
if (Team2Win == 1 | Team2Loss == 1), Team = "Team2"
I'm really struggling with how to do this in dplyr using mutate. I've tried various approaches with ifelse, recode, etc., but I either keep getting errors or results that are not what I want.
What is correct and most efficient way to make this work in dplyr?
mutate(Team = ifelse(is.na(Team1Win) & is.na(Team1Loss), "Team2", "Team1"))- Vloifelsefunction is a great one for this type of use-case - bouncyballifelse. Note that the secondifelseis the next test if the previousifelseis FALSE.df <- df %>% mutate(Condition = ifelse(Condition == 'thing1', 'other-thing1', ifelse(Condition == 'thing2', 'other-thing2', 'default-thing')))- Paul