按日期乘以转置行

Multiply by transposing rows by date

我想转换这个 table :

  Person  startDate    endDate
 Person1 2018-12-31 2019-03-30
 Person2 2018-12-31 2019-01-30
 Person3 2019-02-01 2019-05-30

df1 <- data.frame(Person = paste0("Person", 1:3),
                  startDate = as.Date(c("31 12 2018", "31 12 2018", "01 02 2019"), format = "%d %m %Y"),
                  endDate = as.Date(c("30 03 2019", "30 01 2019", "30 05 2019"), format = "%d %m %Y"),
                  stringsAsFactors = FALSE)

到 table 和 R

简而言之:将开始日期和结束日期之间的时间段垂直转换为月table

谢谢!

这是一种方法

library(dplyr)
library(purrr)
library(lubridate)
df1 %>%
    transmute(Person, Date = map2(dmy(StartDate), dmy(EndDate), ~
                           seq(.x, .y, by = '1 month') %>%
                                 format('%b %Y'))) %>%
    unnest(Date)