summarize with multiple filters

Hello. For future questions, please consider sharing a reproducible example (reprex) as described here. That makes it really easy for community members to help you.

For this question, I've prepared some sample data to illustrate one possible solution. To avoid duplication, you can use group_by() to perform the same operation across each of your three groups like so:

library(dplyr, warn.conflicts = FALSE)

data <- tibble(Var_1 = c(900, 1500, 350, 1200, 750, 100),
               Var_2 = c(90000, 10000, 8500, 4000, 5000, 1500))

head(data)
#> # A tibble: 6 x 2
#>   Var_1 Var_2
#>   <dbl> <dbl>
#> 1   900 90000
#> 2  1500 10000
#> 3   350  8500
#> 4  1200  4000
#> 5   750  5000
#> 6   100  1500

data %>% 
  mutate(Var_1_Category = case_when(Var_1 < 500 ~ "< 500",
                                    Var_1 >= 500 & Var_1 < 1000 ~ "500-1000",
                                    TRUE ~ "> 1000")) %>% 
  group_by(Var_1_Category) %>% 
  summarize(Percentage = sum(Var_1) / sum(Var_2))
#> # A tibble: 3 x 2
#>   Var_1_Category Percentage
#>   <chr>               <dbl>
#> 1 < 500              0.045 
#> 2 > 1000             0.193 
#> 3 500-1000           0.0174

Created on 2020-04-11 by the reprex package (v0.3.0)

1 Like