0

So I have one dataset (DF1) that includes baseball players, the year, and their stats in that year. I have another (DF2) that lists the players, the year, and their salary in that year. I would like to add the salary column information to DF1 when player name AND year match in both datasets.

I tried

DF1$Salary <- DF2$salary[match(Pitching$playerID, Salaries$playerID)]

But realized that if I did this the information was only correct for the first year. I need to only make the match if year and player ID are the same. Can someone help me? Thanks!

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • 1
    `merge(Pitching, Salaries, by = c('playerID', 'year'))` ? – Ronak Shah Apr 24 '20 at 04:47
  • 1
    And I'll pitch in with the alternative using the package `dplyr`: `inner_join(Pitching, Salaries, by=c('playerID','year'))` - do note that if you need a left/full/right join, `merge` uses arguments `all.x` and `all.y`, while `dplyr` as corrosponding verbs `left_join`, `right_join`, and `full_join`. – MrGumble Apr 24 '20 at 05:02
  • 2
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – NelsonGon Apr 24 '20 at 05:26

0 Answers0