I have a tibble with name, region, size and num
I need to get the top 1 names in each region by the sum of num
data <- tribble(
~name, ~region, ~size, ~num,
"joe", "east", "small", 10,
"moe", "east", "small", 20,
"doe", "east", "small", 30,
"joe", "west", "small", 30,
"moe", "west", "small", 20,
"doe", "west", "small", 10
)
result <- data %>%
group_by(name, region) %>%
summarize(total = sum(num)) %>%
top_n(1)
result
This gives the top 1 for each name/region pair (6 rows - too many), but I need the top 1 names for each region (east, doe, 30 and west, joe, 30). What do I need to add?