Objective: to fill the value of a column in 1 Dataframe from values of a 2nd DF.
I have a Dataframe with the following:
DF1
ID | ValueA | Dates |
---|---|---|
Aaaaaa | Na | 02/13 |
Bbbbbb | Na | 03/13 |
Cccccc | Na | 04/13 |
Aaaaaa | Na | 03/14 |
DF2
ID | ValueA | Dates |
---|---|---|
Aaaaaa | 12 | 02/13 |
Bbbbbb | 19 | 03/13 |
Aaaaaa | 15 | 03/14 |
Cccccc | 13 | 04/13 |
I would like to place ValueA from DF2 into valueA of DF1, with match ID and Dates of each entry without it causing an offset because they may not be in the same order which is the current issue I am getting.
DF1$ValueA <- DF2$ValueA
does not seem to do what I want. I have also tried to fuse to tables with no luck.
Any help of direction would be appreciated. Thanks
CodePudding user response:
Try using dplyr
rows_update
-
DF1 <- dplyr::rows_update(DF1, DF2, by = c('ID', 'Dates'))
DF1
# ID ValueA Dates
#1 Aaaaaa 12 02/13
#2 Bbbbbb 19 03/13
#3 Cccccc 13 04/13
#4 Aaaaaa 15 03/14
data
It is easier to help if you provide data in a reproducible format
DF1 <- structure(list(ID = c("Aaaaaa", "Bbbbbb", "Cccccc", "Aaaaaa"),
ValueA = c(NA_character_, NA_character_, NA_character_, NA_character_
), Dates = c("02/13", "03/13", "04/13", "03/14")),
row.names = c(NA, -4L), class = "data.frame")
DF2 <- structure(list(ID = c("Aaaaaa", "Bbbbbb", "Aaaaaa", "Cccccc"),
ValueA = c(12L, 19L, 15L, 13L), Dates = c("02/13", "03/13",
"03/14", "04/13")), row.names = c(NA, -4L), class = "data.frame")