I have a dataset that I want to summarize. First, I want the sum of the home and away games, which I can do. However, I also want to know how many outliers (defined as more than 300 points) are within each subcategory (home, away).
If I wasn't using summarize, I know dplyr
has the count()
function, but I'd like this solution to appear in my summarize()
call. Here's what I have and what I've tried, which fails to perform:
#Test data
library(dplyr)
test <- tibble(score = c(100, 150, 200, 301, 150, 345, 102, 131),
location = c("home", "away", "home", "away", "home", "away", "home", "away"),
more_than_300 = c(FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE))
#attempt 1, count rows that match a criteria
test %>%
group_by(location) %>%
summarize(total_score = sum(score),
n_outliers = nrow(.[more_than_300 == FALSE]))