How to fill in missing dates in an irregular series of dates in R

This recipe helps you fill in missing dates in an irregular series of dates in R

Recipe Objective

How to fill in the missing dates in an irregular series of dates?

In data mining, data preprocessing is the first step which helps to convert the input raw data into useful required data format. Data Cleaning : The raw data may contain irrelevant and missing data (NA values), and hence data cleaning is to be performed. - Missing Data: The missing data can be handled in multiple ways such as: Ignoring the data, filling the data with some constant value, filling the data with a corresponding measure of central tendency like mean/ median. Similarly, in time series analysis the data of "dates" can also be missing from the data set and can be filled using the following example technique. This recipe demonstrates an example of how to fill in missing dates in an irregular series of dates in R.

Step 1 - Generate random data

random_data <- data.frame(date = seq(as.Date("2015-01-01"), as.Date("2020-12-31"), by = "1 month"), value = rnorm(72)) random_data

Step 2 - Remove some data observations

## Remove some observations so we have an incomplete data set remove_data <- random_data[sort(sample(nrow(random_data), 50)), ] remove_data

Step 3 - Define min and max date of the dataframe.

dates <- seq(min(remove_data$date), max(remove_data$date), by = "1 month") dates <- data.frame(date = dates)

Step 4 - Combine the dates and remove_data to fill in the dates

complete_data <- merge(dates, remove_data, by = "date", all.x = TRUE) complete_data

The missing dates get added with corresponding NA values in the dataframe.

{"mode":"full","isActive":false}

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed