Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

filter variable based on prior date value in r

$
0
0

I have a data frame like below :

df <- structure(list(id = c("555900339", "555900339", "555900339", 
                               "555900339", "555900339", "555900327", "555900327", "555900327", 
                               "555703505", "555703379", "555703379", "555703379", "555703379", 
                               "555703379", "555703366", "555702668", "555702668", "555702668", 
                               "555702668", "555702668"), date = c("20200207", "20200207", 
                                                                          "20200207", "20200208", "20200208", "20200207", "20200207", "20200207", 
                                                                          "20200207", "20200207", "20200207", "20200207", "20200207", "20200207", 
                                                                          "20200207", "20200207", "20200208", "20200208", "20200208", "20200208"
                               ), flag_code = c("SLEP", "NCHG", "MOTN", "CIHB", "NCON", "SLEP", 
                                              "NCHG", "MOTN", "INMC", "SLEP", "NCHG", "MOTN", "COFF", "NCON", 
                                              "NCHG", "SLEP", "NOMO", "NCON", "MOTN", "CIHB")), row.names = c(NA, 
                                                                                                              -20L), class = c("tbl_df", "tbl", "data.frame"))

I would like to see how many records ( unique id here ) were preceded by the NCHG flag code for the day prior to the NCON flag. I need to get a something like this

  id          date flag_code
 555900339   20200207  NCHG
 555900339   20200208  NCON     
 555703366   20200207  NCHG      
 555702668   20200208  NCON 

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>