title: How to generate a sequence of monthly dates from a data frame in R?
body: <p>Consider the following data frame (df):</p>
  
  <pre><code>"id"    "date_start"    "date_end"
   a       2012-03-11     2012-03-27
   a       2012-05-17     2012-07-21
   a       2012-06-09     2012-08-18
   b       2015-06-21     2015-07-12
   b       2015-06-27     2015-08-04
   b       2015-07-02     2015-08-01
   c       2017-10-11     2017-11-08
   c       2017-11-27     2017-12-15
   c       2017-01-02     2018-02-03
  </code></pre>
  
  <p>I am trying to create a new data frame with sequences of monthly dates, starting one month prior to the minimum value of "date_start" for each group in "id". The sequence also only includes dates from the first day of a month and ends at the maximum value of "date-end" for each group in "id". </p>
  
  <p>This is a reproducible example for my data frame:</p>
  
  <pre><code>library(lubridate)
  
  id &lt;- c("a","a","a","b","b","b","c","c","c")
  df &lt;- data.frame(id)
  df$date_start &lt;- as.Date(c("2012-03-11", "2012-05-17","2012-06-09", "2015-06-21", "2015-06-27","2015-07-02", "2017-10-11", "2017-11-27","2018-01-02"))
  df$date_end &lt;- as.Date(c("2012-03-27", "2012-07-21","2012-08-18", "2015-07-12", "2015-08-04","2015-08-012", "2017-11-08", "2017-12-15","2018-02-03"))
  </code></pre>
  
  <p>What I have tried to do:</p>
  
  <pre><code>library(dplyr)
  library(Desctools)
  library(timeDate)
  
  df2 &lt;- df %&gt;%
     group_by(id) %&gt;%
     summarize(start= floor_date(AddMonths(min(date_start),-1), "month"),end=max(date_end)) %&gt;%
     do(data.frame(id=.$id, date=seq(.$start,.$end,by="1 month")))
  </code></pre>
  
  <p>The code works perfectly fine for an ungrouped data frame. Somehow, with the grouping by "id" it throws an error message: </p>
  
  <pre><code>Error in seq.default(.$date_start, .$date_end, by = "1 month") : 
  'from' must be of length 1
  </code></pre>
  
  <p>This is how the desired output looks like for the data frame given above:</p>
  
  <pre><code>"id"       "date"    
   a       2012-02-01     
   a       2012-03-01     
   a       2012-04-01     
   a       2012-05-01    
   a       2012-06-01     
   a       2012-07-01     
   a       2012-08-01         
   b       2015-05-01 
   b       2015-06-01 
   b       2015-07-01
   b       2015-08-01  
   c       2017-09-01 
   c       2017-10-01 
   c       2017-11-01
   c       2017-12-01
   c       2018-01-01
   c       2018-02-01
  </code></pre>
  
  <p>Is there a way to alter the code to function with a grouped data frame? Is there an altogether different approach for this operation?</p>