1

I would like to compare dt1 to dt2 such that if dt1 A column value exist when it compare to dt2 A column value it would not add a row or change the value in that row. So when the value are different like shown in the example where dt2 has "4" & "5" in A column it will add "4" & "5" to dt1 table but it would not replace it's "1", "2" or "3" value in dt1 A column since it existed already.

Edit: I would like to use A column in a form of a primary key to check something along the line of if "1" exist in A column in both both table then nothing changes but if in there is a A column value in dt2(Example:"4") but not in dt1 then i wan that row of data to merge with dt1 table

dt1 
A   B   C   
1   4   7   
2   5   8   
3   6   9   

dt2
A   B   C  
1   10  13  
2   12  15  
3   11  14  
4   1   20
5   12  3

dt1
A   B   C   
1   4   7   
2   5   8   
3   6   9    
4   1   20
5   12  3
Arun
  • 116,683
  • 26
  • 284
  • 387
Terry
  • 23
  • 3
  • Relevant post: [How to join (merge) data frames (inner, outer, left, right)?](http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – zx8754 Jul 30 '15 at 07:50

2 Answers2

3

You can bind dt1 with rows from dt2 that don't have A values in dt1 with just one conditional statement

rbind(dt1, dt2[!(dt2$A %in% dt1$A),])
#   A  B  C
# 1 1  4  7
# 2 2  5  8
# 3 3  6  9
# 4 4  1 20
# 5 5 12  3
Rorschach
  • 31,301
  • 5
  • 78
  • 129
2

Or using dplyr, we get the rows that are in 'dt2' that are not in 'dt1' grouped by 'A' with anti_join and rbind with 'dt1' using bind_rows

 library(dplyr)
 anti_join(dt2, dt1, by='A') %>% 
                       bind_rows(dt1, .)

Or another option would be using rbindlist to rbind both the datasets and get the unique based on 'A' column.

 library(data.table)
 unique(rbindlist(list(dt1, dt2)), by='A')

Or as @Arun mentioned in the comments, if we use the devel version of 'data.table' i.e. v1.9.5 (Instructions to install the devel version are here), on option can be used to join the two datasets without specifying the keys to get the rows in 'dt2' that are not in 'dt1' and rbind with the 'dt1'

  rbind(dt1, dt2[!dt1, on="A"])
akrun
  • 874,273
  • 37
  • 540
  • 662