@rdr I think I found a solution that you may like because it keeps the information on teams. Do not forget to mark one of the answers as the solution for the sake of potential future readers.
library(dplyr)
library(tidyr)
df <- data.frame(
Name = c("Mookie Betts","Kevin Pillar",
"Kevin Pillar","Xander Bogaerts"),
Team = c("BOS", "TOR", "SFG", "BOS"),
H = c(7L, 10L, 2L, 9L),
AB = c(20L, 27L, 12L, 32L),
BA = c(0.35, 0.308, 0.167, 0.281)
)
df
Name Team H AB BA
1 Mookie Betts BOS 7 20 0.350
2 Kevin Pillar TOR 10 27 0.308
3 Kevin Pillar SFG 2 12 0.167
4 Xander Bogaerts BOS 9 32 0.281
df %>%
group_by(Name) %>%
summarize(
Teams = paste0(Team, collapse = ", "),
across(c(H, AB, BA), sum)
)
# A tibble: 3 x 5
Name Teams H AB BA
<chr> <chr> <int> <int> <dbl>
1 Kevin Pillar TOR, SFG 12 39 0.475
2 Mookie Betts BOS 7 20 0.35
3 Xander Bogaerts BOS 9 32 0.281