I have two dataframes df1
and df2
. There's a 1:n-relationship between these two dataframes with df2
having multiple entries for each entry in df1
.
My goal is to merge these two dataframes so that all the rows in df1
are repeated and that all the columns in df2
are imported. The foreign key (FK) in df2
refers to the primary key (PK) in df1
# example data:
df1 <- data.frame(PK = c(1,2,3,4,5),
varA = sample(5, replace=T),
varB = sample(5, replace=T))
df2 <- data.frame(FK = c(1,1,2,2,3,3,4,4,4),
varC = seq(9),
varD = seq(9))
The dataframe should be like:
PK | FK | varA | varB | varC | varD
1 1 ... ... 1 1
1 1 ... ... 2 2
2 2 ... ... 3 3
2 2 ... ... 4 4
3 3 ... ... 5 5
3 3 ... ... 6 6
4 4 ... ... 7 7
4 4 ... ... 8 8
4 4 ... ... 9 9
5 NA ... ... NA NA
All the entries in df1
should be there, even if there is no corresponding entry in df2
(varC and varD should be NA then)