So, in the code below, completed_games is a dataframe containing all college football games involving at least 1 FBS team that have been completed up to this point. VoA_Variables is a dataframe containing stats and my ratings and rankings for each team. FCS is a dataframe containing ratings for FCS college football teams that I want to reference whenever Air Force played an FCS team. Basically, I want home_VoA_rating to be the VoA rating of the home team pulled in from VoA_Variables if it is an FBS team and the rating from the FCS df if it is an FCS team. I want the same thing for away_VoA_rating. The filter is working perfectly when I run that without the mutate following it, but I'm running into trouble in the mutate() function and getting an error that says "Error in mutate()
:
! Problem while computing home_VoA_rating = case_when(...)
.
Caused by error in case_when()
:
! TRUE ~ FCS$rating[FCS$team == home_team]
must be length 10 or one, not 0."
AirForce <- completed_games |>
filter(home_team == "Air Force" | away_team == "Air Force") |>
mutate(home_VoA_rating = case_when(home_team %in% VoA_Variables$team ~ VoA_Variables$VoA_Rating[VoA_Variables$team == home_team],
TRUE ~ FCS$rating[FCS$team == home_team]),
away_VoA_rating = case_when(away_team %in% VoA_Variables$team ~ VoA_Variables$VoA_Rating[VoA_Variables$team == away_team],
TRUE ~ FCS$rating[FCS$team == away_team]),
actual_diff = case_when(home_team == 'Air Force' ~ home_points - away_points,
TRUE ~ away_points - home_points),
projected_diff = case_when(home_team == 'Air Force' ~ home_VoA_rating - away_VoA_rating,
TRUE ~ away_VoA_rating - home_VoA_rating),
Resume_Score = projected_diff - actual_diff)
If I take out the case_when statements following home_VoA_rating and away_VoA_rating and replace them with 0 (so, home and away ratings would then equal 0 for each game in the dataframe), the code runs the way it is supposed to. So the issue is just the process of assigning the appropriate rating to the appropriate team based on whether they are an FBS team or not and whether they are home or not.