I have a series of receivers that were deployed for various periods. The dataset looks like this:
| Receiver | start_dt | end_dt |
|---|---|---|
| 1 | 2021-05-19 15:43:00 | 2022-06-19 12:43:00 |
| 2 | 2021-08-19 15:43:00 | 2022-05-10 18:43:00 |
| 3 | 2021-12-19 15:43:00 | 2022-06-19 12:43:00 |
I would like to extract each monthyear that each receiver was deployed within the start and end dates such that each receiver is linked to each of it's respective monthyear's it was active. Here is an example of what I'm trying to achieve:
| Receiver | monthyear |
|---|---|
| 1 | 2021-05 |
| 1 | 2021-06 |
| 1 | 2021-07 |
| 1 | 2021-08 |
| 1 | 2021-09 |
| 1 | 2021-10 |
| 1 | 2021-11 |
| 1 | 2021-12 |
| 1 | 2022-01 |
| 1 | 2022-01 |
| 1 | 2022-02 |
| 1 | 2022-03 |
| 1 | 2022-04 |
| 1 | 2022-05 |
| 1 | 2022-06 |
| 2 | 2021-08 |
| 2 | 2021-09 |
| 2 | 2021-10 |
| 2 | 2021-11 |
| 2 | 2021-12 |
| 2 | 2022-01 |
| 2 | 2022-02 |
| 2 | 2022-03 |
| 2 | 2022-04 |
| 2 | 2022-05 |
| 3 | 2021-12 |
| 3 | 2022-01 |
| 3 | 2022-02 |
| 3 | 2022-03 |
| 3 | 2022-04 |
| 3 | 2022-05 |
| 3 | 2022-06 |
I am not sure if lubridate is the best option here? In my head it seemed relatively simple but I can't seem to make it work with it or using other examples on stack.
Thank you for any help.
reframelets us do most of the work in one line.Here, I convert (if not already) the two datetime columns to datetime (receiving data in year-month-day-hour-minute-second order, hence
ymd_hms), convert those to date withas_date, and convert those to the first of the respective month. Then we can usereframeto make a sequence of monthly rows for each Receiver's range of months.Result (if you like, you could convert to
tsibble::yearmonthor convert to a string YYYY-MM withmutate(monthyear = format(monthyear, "%Y-%m"))