I am currently working with a dataset containing the records of some water-supply tanks that shows the DATE of technical inspections of 5 different tanks (IDENT) and also the TYPE of inspection recorded which can only have two values "READ" when the tank is working properly and "ERROR" when otherwise performing poorly.
| IDENT | DATE | TYPE |
|---|---|---|
| X3 | 30/04/2021 | ERROR |
| X1 | 1/05/2021 | READ |
| X1 | 2/05/2021 | ERROR |
| X4 | 3/05/2021 | READ |
| X9 | 4/05/2021 | ERROR |
| X6 | 5/05/2021 | READ |
| X1 | 6/05/2021 | READ |
| X3 | 7/05/2021 | ERROR |
| X3 | 8/05/2021 | READ |
I have to create a dataframe that can filter and select every TYPE="ERROR" DATE for each water tank (is there is no error recorded on the dateset for a specify tank is not necessary to show it) and show the latest TYPE="READ" DATE prior to the each tank's ERROR and also the latest DATE After each tank's error date, to illustrate I am to achieve this table:
| IDENT | READ_PRIOR | ERROR | POST_READ |
|:-----:|:----------:|:----------:|:---------:|
| X3 | NA | 30/04/2021 | 8/05/2021 |
| X3 | NA | 7/05/2021 | 8/05/2021 |
| X1 | 1/05/2021 | 2/05/2021 | 6/05/2021 |
| X9 | NA | 4/05/2021 | NA |
What Have I tried?
I have started working on this problem by arranging the data set in chronological order by DATE and also grouping by IDENT using the tidyverse package, also I can select the latest DATE for a group using the top_n function but my issue is that I cant seem to find a way to successfully filter or select the latest dates for a tank before and after the reference TYPE="ERROR" so that's where I am bumping my head. Thank you so much for helping me put guys I truly appreciate it.
Code:
df<- tibble::tribble(
~IDENT ~DATE ~TYPE,
"X3", "30/04/2021", "ERROR",
"X1", "1/05/2021", "READ",
"X1", "2/05/2021", "ERROR",
"X4", "3/05/2021", "READ",
"X9", "4/05/2021", "ERROR",
"X6", "5/05/2021", "READ",
"X1", "6/05/2021", "READ",
"X3", "7/05/2021", "ERROR",
"X3", "8/05/2021", "READ")
Thank you so much guys!