Aggregation library(magrittr) set.seed(1) ex_dat <- data.frame(group = rep(1:3, each = 2), y = rnorm(6), x = rnorm(6)) ex_dat ## group y x ## 1 1 -0.6264538 0.4874291 ## 2 1 0.1836433 0.7383247 ## 3 2 -0.8356286 0.5757814 ## 4 2 1.5952808 -0.3053884 ## 5 3 0.3295078 1.5117812 ## 6 3 -0.8204684 0.3898432 Summarise selected columns by group
dplyr::group_by(ex_dat, group) %>% dplyr::summarise(total_y = sum(y)) ## # A tibble: 3 x 2 ## group total_y ## <int> <dbl> ## 1 1 -0.