Guest User

Untitled

a guest
Sep 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. deals <- read.csv("deals_dates.csv")
  2.  
  3. Deal_Id Deal_Name Start_Date End_Date
  4. 1 Samsung Mobile 3/5/2018 6/23/2018
  5. 2 Apple Watch 12/21/2017 2/9/2018
  6.  
  7. Deal_Id Deal_Name Start_Date End_Date
  8. 1 Samsung Mobile 3/5/2018 3/31/2018
  9. 1 Samsung Mobile 4/1/2018 4/30/2018
  10. 1 Samsung Mobile 5/1/2018 5/31/2018
  11. 1 Samsung Mobile 6/1/2018 6/23/2018
  12. 2 Apple Watch 12/21/2017 12/31/2017
  13. 2 Apple Watch 1/1/2018 1/31/2018
  14. 2 Apple Watch 2/1/2018 2/9/2018
  15.  
  16. library(tidyverse)
  17. df1 %>%
  18. mutate_at(vars(ends_with("Date")), mdy) %>%
  19. group_by(Deal_Id, Deal_Name) %>%
  20. nest %>%
  21. mutate(data = map(data, ~
  22. seq(.x$Start_Date, .x$End_Date, by = "1 month") %>%
  23. {x1 <- floor_date(., unit = "month")
  24. x2 <- (x1[-1]-1)
  25. tibble(Start_Date = c(.x$Start_Date[1], x1[-1]),
  26. End_date = c(x2, last(.x$End_Date)))})) %>%
  27. unnest
Add Comment
Please, Sign In to add comment