title: Looping through a list to fill in missing dates (with NAs or 0s in R
body: <p>I am working with a dataset that has dates over multiple IDs. For each ID my goal is to have full years worth of data for however long that ID appears in my dataset. The issue is between all of my IDs I have ~4 continuous years worth of data even though I might have one ID that does not contribute a continuous year.</p>
  <p>I am hoping to loop through each ID value and use the complete function to fill in the dates that that ID specifically is missing, or even identify the Date and ID that is missing from the dataset I have tried this two ways:</p>
  <pre><code>dates &lt;- rep(seq(as.Date(&quot;2004/01/01&quot;), as.Date(&quot;2020/12/31&quot;), &quot;days&quot;), each=20)
  Animal_id &lt;- rep(1:20, times=length(unique(dates)))
  df &lt;- data.frame(dates=dates, id=Animal_id)
  df[,&quot;Year&quot;]&lt;-format(df[,&quot;dates&quot;],&quot;%Y&quot;)
  df &lt;- df[-4, ]
  df &lt;- df[-6, ]
  df &lt;- df[-10, ]
  df &lt;- df[-15, ]
  df &lt;- df[-16, ]
  df &lt;- df[-20, ]
  ##Take out random columns
  ##split the data 
  x&lt;-split(df,df$Animal_id)
  
  for (i in 1:length(x)){
    complete(Date=seq.Date(min(dates),max(dates),by=&quot;day&quot;))
  }
  
  </code></pre>
  <p>which produces this error:</p>
  <p>Error in min(Date) : invalid 'type' (closure) of argument</p>
  <p>My goal is to be able to have NAs for each missing ID and date so I can then look at what IDs have an NA for that missing date.</p>
  <p>I have also tried to figure out which IDs were missing specific dates using a loop through the following method:</p>
  <pre><code>df$combo&lt;-paste(df$Animal_id,df$Year, sep = &quot;-&quot;)
  z&lt;-split(df, df$combo)
  
  missingID&lt;-(NULL)
  
  for (i in 1:length(z)){
    AnimalIDloop&lt;-z[[i]]
    myDataDateID&lt;-AnimalIDloop[,c(&quot;dates&quot;)]
    FullSeqID&lt;-seq.Date(from = min(myDataDateID), to=max(myDataDateID), by=1)
    missingID&lt;-rbind((FullSeqID[!FullSeqID %in% myDataDateID]),AnimalIDloop$Animal_id[2])
    
    ##rbind dataframe created with the column being AnimalID, Other columns = ##second part  
    ##pull out first value from the AnimalID column 
    
    ##missingID&lt;-rbind(MissingID, new rows (above))
  }
  missingID
  
  </code></pre>
  <p>Essentially I am trying to use the Rbind function to create a new dataframe with each of the missing dates, taking the first value from the animal ID column. My goal is to have the ID listed, as well as the date that is missing for that ID ( if an ID is missing multiple days then it would appear multiple times for each date), but the above does not give me the output I am looking for.</p>
  <p>Thanks in advance!</p>
  <p>Edit- I was using the split function and loop because in my dataset not all animal ids appear in every year (i.e. some individuals die or are no longer monitored) I am just trying to ensure that each id has a full year of data for the year that it appears in.</p>