text: Q:
  
  R: Group by 2 columns but only when 2nd column doesn't match
  
  I'm new to R (and dplyr) and I'm currently working with some seasonal football data and need some help. Currently if a player transfers to another club in the same league then the row of data and all metrics is simply duplicated but with a new team_id. However if the player transfers to another league then the metrics are split. 
  For consistency's sake I need to resolve this which means that I have to: 
  
  Group by player_id where comp_id does not match
  
  (football regulations dictate that you can only play for a max. of 2 clubs in a season so this negates further complications and so this simple rule resolves everything)
  so in other words if there are duplicates sum all rows but only if the comp_id differs
  I was trying to do this in dplyr and was hoping that there would be some way of writing this such as: 
  football_data %>%
  group_by(player_id, !comp_id)
  
  but this doesn't work and quite rightly so.
  My other option is to create an additional column based on a composite key (player_id, comp_id) and group_by player_id only where count of composite key = 1, but even then I'm struggling to write this with dplyr. 
  Any suggestions would be very welcome.
  Edit: 
  There's close to a hundred metrics so I'll simplify as per below: 
  player_id   player_name   comp,   team,  metric1, metric2, metric3 .....
  1            Lacazette     Bund   Dort     20       30       20
  1            Lacazette     EPL    Ars      10       15       15
  2            Arnautovic    EPL    Stoke    30       40       30     
  2            Arnautovic    EPL    W. Ham   30       40       30       
  
  so in this example the data metrics are split when a player moves leagues (Lacazette) but not if the transfer is in the same league (Arnautovic).
  So for consistency I need to sum the metrics for the first player but not  for the second player
  resulting in an output: 
  player_id   player_name   comp,   team,  metric1, metric2, metric3 .....
  1            Lacazette     Bund   Dort     30       45       35
  1            Lacazette     EPL    Ars      30       45       35
  2            Arnautovic    EPL    Stoke    30       40       30     
  2            Arnautovic    EPL    W. Ham   30       40       30     
  
  A:
  
  Essentially, you want to 
  
  get the player-level sum of the metric variables, grouped by comp
  join them back into the full dataframe. 
  
  library(tidyverse)
  #> -- Attaching packages ----------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
  #> v ggplot2 2.2.1     v purrr   0.2.4
  #> v tibble  1.4.1     v dplyr   0.7.4
  #> v tidyr   0.7.2     v stringr 1.2.0
  #> v readr   1.1.1     v forcats 0.2.0
  #> -- Conflicts -------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
  #> x dplyr::filter() masks stats::filter()
  #> x dplyr::lag()    masks stats::lag()
  
  df <- data.frame(player_id = c(1, 1, 2, 2), 
                   player_name = c("Lacazette", "Lacazette", "Arnuatovic", "Arnuatovic"), 
                   comp = c("Bund", "EPL", "EPL", "EPL"), 
                   team = c("Dort", "Ars", "Stoke", "W. Ham"), 
                   metric1 = c(20, 10, 30, 30), 
                   metric2 = c(30, 15, 40, 40), 
                   metric3 = c(20, 15, 30, 30))
  
  df %>% 
    distinct(player_id, player_name, comp, .keep_all = T) %>% 
    group_by(player_id) %>% 
    summarize_at(vars(metric1:metric3), funs(sum)) %>% 
    left_join(df %>% select(player_id:team), ., by = "player_id")
  
  #>   player_id player_name comp   team metric1 metric2 metric3
  #> 1         1   Lacazette Bund   Dort      30      45      35
  #> 2         1   Lacazette  EPL    Ars      30      45      35
  #> 3         2  Arnuatovic  EPL  Stoke      30      40      30
  #> 4         2  Arnuatovic  EPL W. Ham      30      40      30
  
  