Reputation: 683
I am calculating passes per shot for a number of games and summarizing by team. There are a few games where zero shots were taken. For this analysis I feel it is more important to replace the 0 with a 1, rather than omit the game. How would I do that in R? Here is my code.
match_df <- data.frame(TeamName = c("Team A", "Team A", "Team A", "Team B", "Team B", "Team B"), Passes = c(325,300,350,250,275,300), Shots = c(10,8,12,5,5,0))
overview <- match_df %>%
mutate(PassesPerShot = Passes / Shots) %>%
select(TeamName, PassesPerShot) %>%
group_by(TeamName) %>%
summarise(across(everything(), ~round(mean(.),digits = 1))) %>%
arrange(desc(PassesPerShot))
Upvotes: 2
Views: 547
Reputation: 101247
I am not sure if a workaround with pmax
makes sense for your case
within(match_df,PassesPerShot <- Passes/pmax(Shots,1))
such that
> within(match_df,PassesPerShot <- Passes/pmax(Shots,1))
TeamName Passes Shots PassesPerShot
1 Team A 325 10 32.50000
2 Team A 300 8 37.50000
3 Team A 350 12 29.16667
4 Team B 250 5 50.00000
5 Team B 275 5 55.00000
6 Team B 300 0 300.00000
Upvotes: 2
Reputation: 887048
We could replace
by 1
library(dplyr)
match_df %>%
mutate(PassesPerShot = Passes / replace(Shots, Shots == 0, 1))
Upvotes: 2