Let’s say we have column with the following years:
2012, 2013, 2014, 2015, 2017, 2018, 2019, 2020, 2021, 2022
Now I need a code which will identify which years is missing (2016 in this case)
>Solution :
You could use setdiff().
setdiff(seq(min(x), max(x)), x)
# [1] 2016
Data
x <- c(2012,2013,2014,2015,2017,2018,2019,2020,2021,2022)
Update
According to the additional request, the code could be extended as
yr <- setdiff(seq(min(x), max(x)), x)
result <- if(length(yr)) yr else "no year is missing"