0

I am looking to determine the max score within a data set but only when looking the group population is above a certain threshold, in this case above 20. sample data:

Race = c("African American", "Asian", "Hispanic", "White")
Population = c(28, 11, 31, 64)
Average_Score = c(65, 82, 49, 75)
df = data.frame(Race, Population, Average_Score)

    Race            Population Average_Score
1 African American      28           65
2 Asian                 11           82
3 Hispanic              31           49
4 White                 64           75

What I would like to do is something like:

df %>% mutate(reference=max(Average_Score)) where Population > 20

however, I need to add a condition so that it only pulls the max(Average_Score) where Population is greater than 20. In this case, instead of returning Average_Score of 82 (because the Asian population is less than 20) it would return Average_Score of 75 (because the highest Average_Score of any group with Population greater than 20 is the Average_Score that coincides with White)

Any help would be greatly appreciated.

2
  • You need max(Average_Score[Population > 20]) Commented Jan 28, 2019 at 18:44
  • 1
    thanks! that worked great! Commented Jan 29, 2019 at 23:03

2 Answers 2

1

I used akrun's suggestion:

max(Average_Score[Population > 20])

but nycrefugee's also worked:

df %>% 
  filter(Population > 20) %>%
  filter(Average_Score == max(Average_Score))
Sign up to request clarification or add additional context in comments.

Comments

0

a dplyr solution that might offer some more flexibility.

df %>% 
  filter(Population > 20) %>%
  filter(Average_Score == max(Average_Score))

   Race Population Average_Score
1 White         64            75

1 Comment

Thanks! The way that the code was set up, I ended up using the code suggested by akrun above: max(Average_Score[Population > 20])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.