0

I have a df1:

Encounter    Score1
33           4
44           6
66           9
77           2
89           3
100          1
110          4
120          4
104          2

I have a second (and third) smaller df2 that look like:

Encounter    Age     Kids
33           40      2
66           43      9
89           18      3
100          25      1

I would like to merge these two data.frames by "Encounter" but when I am matching them, my new dataframe has more rows that df2.

I am using new <- merge(df2,df1, by="Encounter") but the new df has more rows than the original df2. How can I restrict the new df to just the 4 original rows of df2?

Evan
  • 1,477
  • 1
  • 17
  • 34
  • 2
    `merge(df2, df1, by = "Encounter", all = FALSE, all.x = TRUE)` keeps all rows in `df2` (even the ones that have no matched rows in `df1`). `all.x = FALSE` keeps only matched rows in both `df2` and `df1`. What you need should be one of them. This question is likely addressed in greater details [here](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right?rq=1). – ekoam Jan 11 '22 at 04:00
  • @ekoam, I used all.x = TRUE and I still have more rows in the new df than df2. Help? – Evan Jan 13 '22 at 09:51
  • Are you *sure* that's the order you used in `merge`? I'm doing the same thing and get 4 rows i.e. a left-join of df1 onto df2 – camille Jan 13 '22 at 17:58

0 Answers0