I want to delete events that are not following a date sequence as it shown in dataframe A. For example, events 2 and 4 are not having a sequence in date, there are some days missing. Therefore I would like to assign NAs to these events (As it is in the desire_output column), or just delete them from the dataframe.
Event<-c(1,1,1,2,2,2,2,3,3,3,4,4,4,4)
Dates<- as.Date(c("2018-10-22", "2018-10-23", "2018-10-24", "2019-01-03", "2019-01-04", "2019-01-06", "2019-01-07", "2019-05-11", "2019-05-12", "2019-05-13", "2020-02-21", "2020-02-23", "2020-02-27", "2020-02-28"))
Desire_output<- as.Date(c("2018-10-22", "2018-10-23", "2018-10-24", NA, NA, NA, NA, "2019-05-11", "2019-05-12", "2019-05-13", NA, NA, NA, NA))
A<- data.frame(Event, Dates, Desire_output)
Event Dates Desire_output
1 1 2018-10-22 2018-10-22
2 1 2018-10-23 2018-10-23
3 1 2018-10-24 2018-10-24
4 2 2019-01-03 <NA>
5 2 2019-01-04 <NA>
6 2 2019-01-06 <NA>
7 2 2019-01-07 <NA>
8 3 2019-05-11 2019-05-11
9 3 2019-05-12 2019-05-12
10 3 2019-05-13 2019-05-13
11 4 2020-02-21 <NA>
12 4 2020-02-23 <NA>
13 4 2020-02-27 <NA>
14 4 2020-02-28 <NA>
Or just delete them from the dataframe:
Event Dates Desire_output
1 1 2018-10-22 2018-10-22
2 1 2018-10-23 2018-10-23
3 1 2018-10-24 2018-10-24
8 3 2019-05-11 2019-05-11
9 3 2019-05-12 2019-05-12
10 3 2019-05-13 2019-05-13
Any good idea to approach this problem?
na.omit(A)orsubset(A, !is.na(Desire_output))ordplyr::filter(A, !is.na(Desire_output))