I have a main DF
(DF_1
) that I frequently add new rows. However, before adding, I need to verify that the information that is in DF_1
(ID
) is also in this new file (DF_2
). My key is the ID.
DF_1
:
ID <- c(111,222,333,444,555,666)
CAR <- c('BMW','BMW','BMW','FERRARI','FERRARI','VW')
DATE <- c('OCT','NOV','DEC','OCT','DEC','DEC')
TIME <- c(10,12,15,7,11,14)
QUANTITY <- c(115,101,95,88,130,101)
DF_1 <- data.frame(ID,CAR,DATE,TIME,QUANTITY)
Below, DF_2 shows that IDs 222 and 444 are already in DF_1. For some reason, these IDs have changed.
DF_2
:
ID <- c(222,444,777,888)
CAR <- c('FORD','TOYOTA','SUBARU','NISSAN')
DATE <- c('JAN','FEB','APR','MAY')
TIME <- c(20,33,45,79)
QUANTITY <- c(12,66,31,15)
DF_2 <- data.frame(ID,CAR,DATE,TIME,QUANTITY)
Thus, I need the same IDs (in this example 222 and 444) to be replaced and the new IDs to be included, as the result I expect in DF_FINAL
.
DF_FINAL
ID <- c(111,222,333,444,555,666,777,888)
CAR <- c('BMW','FORD','BMW','TOYOTA','FERRARI','VW','SUBARU','NISSAN')
DATE <- c('OCT','JAN','DEC','FEB','DEC','DEC','APR','MAY')
TIME <- c(10,20,15,33,11,14,45,79)
QUANTITY <- c(115,12,95,66,130,101,45,79)
DF_FINAL <- data.frame(ID,CAR,DATE,TIME,QUANTITY)
How do I replace all rows of the same IDs and add the different IDs?
CodePudding user response:
We may use rows_upsert
from dplyr
library(dplyr)
rows_upsert(DF_1, DF_2)