My data looks like this:
id1 id2 date1 date2 len
1 3 2021-01-01 2020-12-01 2
2 3 2021-02-02 2020-12-31 1
3 5 2021-03-01 2020-09-30 3
4 5 2021-03-03 2021-01-30 4
...
For each id2
there is exactly 2 rows. If we let A
represent the first of these 2 rows and B
the second, I would like to obtain this:
id2 A.date1 A.date2 B.date1 B.date2 A.len B.len
3 2021-01-01 2020-12-01 2021-02-02 2020-12-31 2 1
5 2021-03-01 2020-09-30 2021-03-03 2021-01-30 3 4
I thought there might be a way to do this with pivot_wider
but I haven't been able to do it.
Here is the toy data:
data.frame(id1 = c(1, 2, 3, 4), id2 = c(3, 3, 5, 5), date1 = as.Date(c("2021-01-01", "2021-02-02", "2021-03-01", "2021-03-03")),
date2 = as.Date(c("2020-12-01", "2020-12-31", "2020-09-30", "2021-01-30")), len = c(2,1,3,4))