0

I have 2 dataframes:

stats_20 = data.frame(name = c('Abby', 'Ben', 'Casey'), goals = c(4, 6, 3), assists = c(3, 2, 5))
stats_21 = data.frame(name = c('Abby', 'Ben', 'David'), goals = c(3, 3, 8), assists = c(2, 5, 4))

I want to combine the two into a "Total" dataframe similar to:

total_stats
   name      goals     assists
1  Abby          7           5
2  Ben           9           7
3  David         8           4

I don't care about names that don't appear in stat_21 (e.g., Casey).

jmbaacke
  • 3
  • 2

1 Answers1

0

If you want to aggregate the two then use dplyr and do something like:

rbind( stats_20, stats_21 ) %>% group_by( names ) %>% summarize( gl = sum(goals) , ass=sum(assists) )

If you need to first get rid of anything in stats_21 that isn't in stats_20 then look at dplyr::outer_join to see how to flag the records to delete.

JoshK
  • 337
  • 4
  • 16