Computing a binomial col for each identical row

Hi all! Hoping someone can help me.

So I'm working with a baseball dataset, that has 2 rows for each game (one row for home team one row for away team). The step in the project that I'm stuck on is trying to create a new column that computes either a "1" or a "0" for each game based on the pred_era. Meaning for each Game.ID, if the pred_era is greater then the other pred_era, the 3rd column would be a "0" for losing otherwise it would be a "1" for winning. I assume a for loop would be the correct approach? But I'm getting stuck. Can anyone help?

Screenshot 2020-12-28 141259

So ideally the new column would have a "1" for the first row (pred_era = 3.627) and a "0" for the 2nd row (pred_era = 6.96). Then the 3rd row would have a "1" with the 4th row having a "0" and so on.

such as: Screenshot 2020-12-28 141547

A for loop could work, but you can use tidyverse functions to automate that:

# Create reproducible example data
set.seed(10)
dat <- data.frame(Game.ID = rep(LETTERS[1:3], each=2),
                  pred_era = runif(6, min=1, max=7))

dat
#>   Game.ID pred_era
#> 1       A 4.044869
#> 2       A 2.840611
#> 3       B 3.561446
#> 4       B 5.158612
#> 5       C 1.510816
#> 6       C 2.352620


library(dplyr)

dat %>%
  group_by(Game.ID) %>%
  mutate(results = case_when(pred_era == max(pred_era) ~ 0,
                             pred_era == min(pred_era) ~ 1))
#> # A tibble: 6 x 3
#> # Groups:   Game.ID [3]
#>   Game.ID pred_era results
#>   <chr>      <dbl>   <dbl>
#> 1 A           4.04       0
#> 2 A           2.84       1
#> 3 B           3.56       1
#> 4 B           5.16       0
#> 5 C           1.51       1
#> 6 C           2.35       0

Created on 2020-12-31 by the reprex package (v0.3.0)

Thank you! I was not aware of the case_when function. That helped a lot! Thank you and I hope you have a happy new year!