'Trying to create monthly counts but receiving error with lubridate

I am working with time series data and trying to count particular events across multiple years. I want to create a new.dataframe that shows the total count of events for each month/year, including 0 for months that have no events.

The following code worked for one set of data but is not working for two remaining datasets that are set up exactly the same.

Error returned:

Error in seq.int(r1$mon, 12 * (to0$year - r1$year) + to0$mon, by) :
'from' must be a finite number

Code:

new.dataframe <- original.dataframe %>%      
  group_by(var1, var2, .drop = FALSE) %>% 
  drop_na(date_var) %>% 
  mutate(month = lubridate::floor_date(date_var, unit = "months")) %>%  
  count(month) %>%                          
  complete(
    month = seq.Date(
      min(month, na.rm=T),     # include all months with no cases reported
      max(month, na.rm=T),
      by="month"),
    fill = list(n = 0))
new.dataframe <- new.dataframe %>% rename (monyear = month)
new.dataframe$monyear <- format(new.dataframe$monyear, "%m/%Y")

The code runs fine if I remove the following line, however I want these variables:

group_by(var1, var2, .drop = FALSE) %>% 

Does any one have any suggestions for that line of code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source