I have two data frames containing some survey data with two columns of ID. One data frame has data on one year and the other on another year. One of the data frame has a variable that the other has not. Here's an example of these two data frames:
df1 <- data.frame(id1 = c(1, 1, 2, 2, 3, 3),
id2 = c(1, 2, 1, 2, 1, 2),
name = c("foo1", "foo2", "foo3", "foo4", "foo5", "foo6"),
variable = c(100, 110, 120, 130, 140, 150),
year = rep(2019, 6))
df2 <- data.frame(id1 = c(1, 1, 2, 2, 3, 3),
id2 = c(1, 2, 1, 2, 1, 2),
variable = c(200, 210, 220, 230, 240, 250),
year = rep(2020, 6))
I would like to have a unique df that contains two observations per person: the observation for 2019 and the one for 2020. Here's what I tried:
total <- bind_rows(df1, df2)
total <- total[order(total$id1, total$id2, total$year), ]
total <- total[c(1, 2, 3, 5, 4)]
and the df I obtain:
id1 id2 name year variable
1 1 1 foo1 2019 100
7 1 1 <NA> 2020 200
2 1 2 foo2 2019 110
8 1 2 <NA> 2020 210
3 2 1 foo3 2019 120
9 2 1 <NA> 2020 220
4 2 2 foo4 2019 130
10 2 2 <NA> 2020 230
5 3 1 foo5 2019 140
11 3 1 <NA> 2020 240
6 3 2 foo6 2019 150
12 3 2 <NA> 2020 250
There are two issues here: the first one is that there's a NA in 50% of the cells of name whereas I would like the names to be duplicated; the second issue is that it takes 3 steps (more if I want to duplicate the names).
Is there a function that automatically merges the two df the way I want? I would prefer a function included in the tidyr package, but any other solution is accepted.
I know that there are many questions about merging two data frames but so far, I didn't find what I want and I can't find a tidyr function for that.
Edit: expected output:
id1 id2 name year variable
1 1 1 foo1 2019 100
7 1 1 foo1 2020 200
2 1 2 foo2 2019 110
8 1 2 foo2 2020 210
3 2 1 foo3 2019 120
9 2 1 foo3 2020 220
4 2 2 foo4 2019 130
10 2 2 foo4 2020 230
5 3 1 foo5 2019 140
11 3 1 foo5 2020 240
6 3 2 foo6 2019 150
12 3 2 foo6 2020 250