dropping missing values while using dplyr group_by %>% summarise(??? = n())

pbp1920_clutch %>%
group_by(Offense) %>%
summarise('FG%' = mean(ShotOutcome, na.rm = TRUE), ShotOutcome = n())

This code calculates the mean of ShotOutcome without missing values, but counts the ShotOutcome with missing values included. I wanted to use na.rm = TRUE for ShotOutcome = n(), but it doesn't seem to work. Is there any way to drop missing values when counting the number of factors using group_by and summarise()?

sum(!is.na(ShotOutcome))

2 Likes

Hi @skyfall

You can remove the NAs earlier in your code:

pbp1920_clutch %>%
  drop_na(ShotOutcome) %>% 
  group_by(Offense) %>%
  summarise('FG%' = mean(ShotOutcome), ShotOutcome = n())
1 Like

Thank you! It worked

Thank you! It worked.

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.