I have a time series as a data frame as follows:

```
date          country      value       id
1/1/2020       A            .2         Cv
1/2/2020       A             0         Cv
1/3/2020       A             0         Cv 
.....         ...           ....       ...
2/10/2020      A              2        ...
2/11/2020      A              0        Cv
3/11/2020.     A              0        Cv
4/11/2020      A              0        Cv
5/11/2020      A              3        Cv
6/11/2020      A              4        Cv
7/11/2020      A              6        Cv
8/11/2020      A              7        Cv

```

I want to remove all values before the last sequence of zero that we have in the data frame:
I tried following code:

```
test <- ddply(df,.(id),function(x){
  temp_country_data <- ddply(x, .(country),function(y){
    temp_data <- data.frame(y) %>% arrange(date) %>% group_by("id","country") 
dat<-temp_data
    ToRemove <- apply(dat, 2, function(colmn) {
      row.zeros <- which(colmn == 0) # rows with zeros
      if(length(row.zeros) > 0) { # if we found any
        # which of them is the last double
        last.doubles <- max(which(diff(row.zeros) == 1))
        leftof.last.doubles <- "if"(length(last.doubles) > 0, # if double exists1:(row.zeros[last.doubles]-1), # all rows before
                                    NULL) # else nothing
        # remove rows with single zeros and all rows before double consecutive 
        unique(c(row.zeros, leftof.last.doubles)) }
      temp_data<-dat[-unlist(ToRemove),]
    temp_data = temp_data[,c("date", "id", "country", "value")]
    temp_data
  }, .parallel = T)
  temp_country_data
}, .progress = 'text')

```

However it removes only zero values which i do not want.
I want that output be like as follows: Also i want to interval 2 days after the final sequence of zeros:

```
7/11/2020      A              6        Cv
8/11/2020      A              7        Cv

```

......

Also I tried this but still I am not getting the result:

```
test3 <- ddply(df,.(id),function(x){
  temp_country_data <- ddply(x, .(country),function(y){
    temp_data <- data.frame(y) %>% arrange(date) %>% group_by("id","country") 
    temp_data<- temp_data%>% mutate(flag_0 = ifelse(value == 0,1,0),flag_0_cum = cumsum(flag_0)) %>% 
      filter(flag_0_cum == max(flag_0_cum)) %>% 
      filter(round(value,3) != 0) %>% 
      select(-flag_0 , -flag_0_cum) %>%
      slice(3:n())
    temp_data = temp_data[,c("date", "short_id", "country", "raw_de")]
    library(lubridate)

    temp_data <- temp_data %>% 
      group_by(country, id) %>%                          
      mutate(DATE = ymd(date), 
             day_flag = if_else(DATE == (lag(DATE) + days(1)), 1, 0))

temp_data<- temp_data %>% filter(!is.na(day_flag))
temp_data<- temp_data %>% 
  mutate(flag_0 = ifelse(day_flag == 0,1,0),
         flag_0_cum = cumsum(flag_0)) %>% 
  filter(flag_0_cum == max(flag_0_cum)) %>% 
  filter(day_flag != 0) %>% 
  select(-flag_0 , -flag_0_cum) %>%
  slice(3:n())

   temp_data
  }, .parallel = T)
  temp_country_data
}, .progress = 'text')

```

I have added another column into my dataframe to flag the consecutive rows as 1 and not consecutive as zero .

could you please let me know where is the problem.