I have two data frames with different lengths in rows and columns
data.frame(
stringsAsFactors = FALSE,
Date = c("01/01/2000", "01/01/2010", "01/01/2020"),
Germany = c(5, 8, 9),
France = c(4, NA, 7),
Luxembourg = c(10, 6, 3)
) -> df1
data.frame(
stringsAsFactors = FALSE,
Date = c("01/01/1990", "01/01/2000", "01/01/2010", "01/01/2020"),
Germany = c(1, 9, 7, NA),
France = c(10, 3, 9, 6),
Luxembourg = c(10, NA, NA, 7),
Belgium = c(NA, 8, 1, 9)
) -> df2
I have to create a third df (df3) where,
- NA values of df1 are replaced with the values of df2 by matching IDs and Dates and viceversa (the NA from df2 replaced by df1)
- The values of df1 are priority (=TRUE)
- All those columns that are not in one data frame (like Belgium in this case) should also be included in the df3
df3 should look like this:
Any help would be greatly appreciated

