Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to detect missing year in time series data in R?

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading