I'd like to detect if there are non-consecutive years in my Dataframe. For instance:
Ticker Year Gap
A 2017-01-01
A 2018-01-01 OK
A 2019-01-01 OK
A 2021-01-01 NOT OK
A 2022-01-01 OK
AAPL 2019-01-01
AAPL 2020-01-01 OK
AAPL 2021-01-01 OK
AAPL 2022-01-01 OK
AAPL 2023-01-01 OK
What I've done is the following:
data %>%
dplyr::select(Year, Ticker) %>%
group_by(Ticker) %>%
mutate(diff = Year - lag(Year))
diff is a column returning the number of days, hence if the number of days is higher than 366 I just return NOT OK.
My issue is that "diff" is also computing the number of days between the first Year from AAPL and the last year from A while I thought the "group by" would only allow the computation of year difference within groups. For instance, on the row of AAPL 2019-01-01, we would have a negative number of days since the row above, A, is dated on the 2022.
Any helps would be appreciated, thanks.