title: Including group_by in Custom Function R
body: <p>I have a custom function that creates new rows, where it copies the data from row one and adds rows equal to a number in a specific column. Right now, the function works well if there is only one data entry per id. What I need is for the function to work when the data has multiple rows for one id. </p>
  
  <p>My data includes id which is the persons id; Stage which is the stage the person is in; Start/ End which is the Start and End date; MonthDiff which is the difference between the start and end date, and a Censor which is equal to 0 or 1.</p>
  
  <p>I need the function to be grouped by Stage and to copy rows down equal to the month diff in that stage and then restart. </p>
  
  <p>What I have so far:</p>
  
  <pre><code>df&lt;-data.frame(id=c('A','A','A'),
             Stage=c(1,2,3),
             Start=c(as.Date('2014-01-01'),as.Date('2016-01-01'),as.Date('2019-01-01')),
             End=c(as.Date('2015-12-31'),as.Date('2018-12-31'),as.Date('2020-02-01')),
             MonthDiff=c(23,35,13),
             Censor=c(0,0,1))
  
  PLPP &lt;- function(data, id,Stage, period, event) 
  {stopifnot(is.matrix(data) || is.data.frame(data))
   stopifnot(c(id, period, event) %in% c(colnames(data), 1:ncol(data)))
  
   if (any(is.na(data[, c(id, period, event)]))) {
  stop("PLPP cannot currently handle missing data in the id, period, or event variables")
  }
         period = {
         index &lt;- rep(1:nrow(data), data[, period])
         idmax&lt;-cumsum(data[, period])
         reve &lt;- !data[, event]
         dat &lt;- data[index, ]
         dat[, period] &lt;- ave(dat[, period], dat[, id], FUN = seq_along)
         dat[, event] &lt;- 0
         dat[idmax, event] &lt;- reve}
  
  
  rownames(dat) &lt;- NULL
   return(dat)
  }
  
  tpp&lt;-PLPP(df,id='id',Stage = 'Stage',period = 'MonthDiff',event = 'Censor')
  
   test&lt;-df%&gt;%group_by(Stage)%&gt;%do(tpp)
  </code></pre>
  
  <p>My problem with the current code is that the group_by statement isn't restarting at the new Stage.</p>
  
  <p>Some references I've tried: <a href="https://stackoverflow.com/questions/48983225/how-to-use-group-by-in-a-function-in-r">how to use group_by in function</a> ;
  <a href="https://stackoverflow.com/questions/44659943/using-dplyr-group-by-in-a-function">Using dplyr group_by in function</a></p>