Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- deals <- read.csv("deals_dates.csv")
- Deal_Id Deal_Name Start_Date End_Date
- 1 Samsung Mobile 3/5/2018 6/23/2018
- 2 Apple Watch 12/21/2017 2/9/2018
- Deal_Id Deal_Name Start_Date End_Date
- 1 Samsung Mobile 3/5/2018 3/31/2018
- 1 Samsung Mobile 4/1/2018 4/30/2018
- 1 Samsung Mobile 5/1/2018 5/31/2018
- 1 Samsung Mobile 6/1/2018 6/23/2018
- 2 Apple Watch 12/21/2017 12/31/2017
- 2 Apple Watch 1/1/2018 1/31/2018
- 2 Apple Watch 2/1/2018 2/9/2018
- library(tidyverse)
- df1 %>%
- mutate_at(vars(ends_with("Date")), mdy) %>%
- group_by(Deal_Id, Deal_Name) %>%
- nest %>%
- mutate(data = map(data, ~
- seq(.x$Start_Date, .x$End_Date, by = "1 month") %>%
- {x1 <- floor_date(., unit = "month")
- x2 <- (x1[-1]-1)
- tibble(Start_Date = c(.x$Start_Date[1], x1[-1]),
- End_date = c(x2, last(.x$End_Date)))})) %>%
- unnest
Add Comment
Please, Sign In to add comment