How to find difference between two dates in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to find difference between two dates in R?

This recipe helps you find difference between two dates in R

0

## Recipe Objective

The date variable/column in a dataset is of utmost importance in terms of carrying out a Time series analysis and exploratory data analysis. ​

In R, most of the times a Date-format variable is read as a character type from any dataset. If you want to carry out any sort of analysis w.r.t dates then you must convert this character type variable to date.

In this Recipe, you will learn how to find the difference between two dates in R ​

## Step 1: Create 2 date variables

We will create 2 date variable in Date format (YYYY-MM-DD) using as.Date()

``` date_1 = as.Date("2020-05-01") date_2 = as.Date("2020-06-01") ```

## Step 2: Difference between two dates

We will use difftime() function to carry out this task.

Syntax: difftime(date1, date2, units = ) ​

1. date1: Date 1 input
2. date2: Date 2 input
3. units: difference in "days", "weeks"
``` diff_dates = difftime(date_2,date_1, units = "days") diff_dates ```
```Time difference of 31 days
```

Note: we mostly convert the difference in terms of a numeric value by using as.numeric() function

``` diff_dates = as.numeric(diff_dates) diff_dates ```
```31
```

#### Relevant Projects

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

##### Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

##### Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

##### Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

##### Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

##### Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

##### Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.