Glance ahead at the solution
For a given data set ds
, defined as
ds_in <-
tibble::tribble(
~id, ~codes_1, ~date_1, ~codes_2, ~date_2,
1, "22" , "2015-01-01", "22,33" , "2021-01-01",
2, "44,55", "2015-01-01", "44,55,66", "2021-01-01",
3, "77,88", "2015-01-01", "77,99" , "2021-01-01",
4, "100" , "2015-01-01", "100" , "2021-01-01",
)
where id
identifies hromadas (ATC) and codes_*
contain the list of radas that comprise the hromada at that time (date_*
)
> ds_in
# A tibble: 4 x 5
id codes_1 date_1 codes_2 date_2
<dbl> <chr> <chr> <chr> <chr>
1 1 22 2015-01-01 22,33 2021-01-01
2 2 44,55 2015-01-01 44,55,66 2021-01-01
3 3 77,88 2015-01-01 77,99 2021-01-01
4 4 100 2015-01-01 100 2021-01-01
please develop a solution that would transform the data into the following form:
> ds_out
# A tibble: 14 x 3
id date code
<dbl> <chr> <dbl>
1 1 2015-01-01 22
2 1 2021-01-01 22
3 1 2021-01-01 33
4 2 2015-01-01 44
5 2 2015-01-01 55
6 2 2021-01-01 44
7 2 2021-01-01 55
8 2 2021-01-01 66
9 3 2015-01-01 77
10 3 2015-01-01 88
11 3 2021-01-01 77
12 3 2021-01-01 99
13 4 2015-01-01 100
14 4 2021-01-01 100
Notes & Thoughts
- Let's try to stay in the tidyverse, if possible.
- I think
purrr
package might be useful here, but not sure
- Use
pivot_longer
for wide-to-long transformation