0

I am looking for a simple R command which will do the following. I have two tables A and B with three columns each- ID,YEAR,VALUE. I want to find out the rows from table B which have the same ID as table A, and have years which are not in table A. I then want to add these rows to A.

Table A
---------------------
| ID | YEAR | VALUE |
---------------------
|  1 | 2006 |   A   |
--------------------
|  1 | 2007 |   B   |
---------------------
|  1 | 2009 |   D   |
---------------------
|  1 | 2010 |   E   |
---------------------

Table B

---------------------
| ID | YEAR | VALUE |
---------------------
|  1 | 2006 |   A   |
--------------------
|  1 | 2007 |   B   |
---------------------
|  1 | 2008 |   C   |
---------------------
|  1 | 2009 |   D   |
---------------------
|  1 | 2010 |   E   |
---------------------

In the output table, only the third row in table B gets appended to table A.

I found this solution- Compare two table and adding row based on condition but it does not address multiple conditions.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
M1001
  • 27
  • 5

1 Answers1

0

We can use anti_join to get rows in B which is not present in A.

difft <- dplyr::anti_join(B, A, by = c('ID', 'YEAR'))
difft
#  ID YEAR VALUE
#1  1 2008     C

then we can rbind this row to A.

rbind(B, difft) 

#  ID YEAR VALUE
#1  1 2006     A
#2  1 2007     B
#3  1 2008     C
#4  1 2009     D
#5  1 2010     E
#6  1 2008     C
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213