0

I have two csvs, one complete one and the other which is partial but has more accurate numbers that I would like to merge with the complete one. Basically wherever the partial one matches up with the complete one, I want to overwrite that value with the data from the partial file.

Here is an example of the two files and what I want the final result to look like...

**complete.csv**
id  value
1    0.81
2    0.74
3    0.77
4    0.75
5    0.85

**partial.csv**
id  value
2    0.99
3    0.99
5    0.99

and this is how the I want the final file...

**merged.csv**
id  value
1    0.81
2    0.99
3    0.99
4    0.75
5    0.99

Of course this is a small example. The actual partial.csv has about 500 values I want to replace into the complete csv. How would I go about doing this?

chrischrischris
  • 57
  • 1
  • 1
  • 6
  • 1
    `complete$value[match(complete$id, partial$id)] <- partial$value[match( partial$id,complete$id)]`? – Jaap Jun 12 '17 at 18:46
  • 1
    or with the `data.table`package: `setDT(complete)[setDT(partial), on = 'id', value := i.value]` – Jaap Jun 12 '17 at 18:47
  • 1
    @Jaap. typo in first suggestion. `complete$value[match(partial$id, complete$id)] <- partial$value[match(complete$id, partial$id, nomatch=0)]` will work. – lmo Jun 12 '17 at 18:54
  • Thank you both, worked flawlessly! – chrischrischris Jun 12 '17 at 19:12

0 Answers0