0

I have two datasets, db1 and db2, like the following ones:

db1
+---------+-------+-------+------+------+-----------------+
|  Authors| IDs   | Title | Year | ISSN | Other columns...|
+---------+-------+-------+------+------+-----------------+
|  Abad J.| 16400 |   1   | 2014 |14589 |                 |
|  Ares K.| 70058 |   2   | 2012 |15874 |                 |
|  Anto E.| 71030 |   3   | 2011 |16999 |                 |
|  A Banul| 57196 |   1   | 2011 |21546 |                 |
|  A Berat| 56372 |   2   | 2011 |12554 |                 |
+---------+-------+-------+------+------+-----------------+

and

db2
+---------+-------+-------+------+------+-------+---------------------------+
|  Authors| IDs   | Title | Year | ISSN | IF    | Other different columns...|
+---------+-------+-------+------+------+-------+---------------------------+
|  Abad J.| 16400 |   1   | 2013 |14589 |  2,3  |                           |
|  Ares K.| 70058 |   2   | 2012 |15874 |  3,3  |                           |
|  Anto E.| 71030 |   3   | 2011 |14587 |  1,2  |                           |
|  A Banul| 57196 |   1   | 2011 |21546 |  7,8  |                           |
|  A Berat| 56372 |   2   | 2011 |75846 |  4,5  |                           |
+---------+-------+-------+------+------+-------+---------------------------+

Basically, what i want is to add to db1 the column IF from db2 when the two columns Year and ISSN have the same values. So what i want to achive is the following output in my example:

db1
+---------+-------+-------+------+------+-------+----------------+
|  Authors| IDs   | Title | Year | ISSN | IF    |Other columns...|
+---------+-------+-------+------+------+-------+----------------+
|  Abad J.| 16400 |   1   | 2014 |14589 |  NA   |                |
|  Ares K.| 70058 |   2   | 2012 |15874 |  3,3  |                |
|  Anto E.| 71030 |   3   | 2011 |16999 |  NA   |                |
|  A Banul| 57196 |   1   | 2011 |21546 |  7,8  |                |
|  A Berat| 56372 |   2   | 2011 |12554 |  NA   |                |
+---------+-------+-------+------+------+-------+----------------+

i have tried with merge but, since i have also different columns, i obtain a very big dataset.

What i want is to use the function match but with more than one condition applied at the same time.

Any guess ?

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

2
dplyr::left_join(db1, db2 %>% dplyr::select(Year, ISSN, IF))

This should work providing the two dataframes have no other columns in common besides the ones you've shown here.

Keith McNulty
  • 952
  • 1
  • 6
  • 17
  • Maybe, write it easier as: `db1 %>% leftjoin(db2 %>% select(Year, ISSN, IF), by = c("Year", "ISSN")))` – Georgery Feb 20 '20 at 09:17