Difference between two dates in R by days, weeks, months and years

In Order to get difference between two dates in R by days, weeks, months and years.  We will be using difftime() function. difftime() function takes days as argument to find difference between two dates in R in days.  difftime() function takes months as argument to find difference between two dates in R in months. difftime() function takes weeks as argument to find difference between two dates in R in weeks, same for quarter, years and so on Let’s see how to

  • Find difference between two dates in R by days – calculate number of days between two dates with an example.
  • Difference between two dates in R by weeks – calculate number of weeks between two dates with an example.
  • Difference between two dates in R by months – calculate number of months between two dates with an example.
  • Difference between two dates in R by years – calculate number of years between two dates with an example.
  • Difference between two dates in R by Quarters – calculate number of quarters between two dates with an example.

Let’s first create the dataframe.

### Create Data Frame

df1 = data.frame (
  Name =c('Annie','Catherine','Teresa','Peterson','Richard','joe'),
  Date_of_birth = as.Date(c('1995-06-16','1991-04-19','1993-07-22','1990-03-26','1991-05-12','1992-09-13')), 
  Date_of_joining = as.Date(c('2018-06-03','2018-06-03','2018-06-03','2018-06-036','2018-06-03','2018-06-03'))) 
df1

dataframe df1 will be

Difference of two dates in R by days, weeks, months, quarter, year 1

 

 

Get difference between two dates in R by days with an example:

Difference between two dates in R can be calculated using difftime function with argument units = “days” as shown below

# Difference in days	

df1$diff_in_days = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "days")) 
df1

So the resultant data frame will be

Difference of two dates in R by days, weeks, months, quarter, year 2

 

 

Get difference between two dates in R by weeks with an example:

Difference between two dates in R by weeks can be calculated using difftime function with argument units = “weeks” as shown below

# Difference in weeks
	
df1$diff_in_weeks = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "weeks"))
df1

So the resultant data frame will be

Difference of two dates in R by days, weeks, months, quarter, year 3

 

 

Get difference between two dates in R by months with an example:

Difference between two dates in R by months can be calculated using difftime function in roundabout way with argument units = “days” divided by (365.25/12) as shown below

# difference in months

df1$diff_in_months = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units ="days"))/(365.25/12)
df1

So the resultant data frame will be

Difference of two dates in R by days, weeks, months, quarter, year 4

 

 

Get difference between two dates in R by year with an example:

Difference between two dates in R by year can be calculated using difftime function in roundabout way with argument units = “weeks” divided by 52.25 as shown below

# difference in years

df1$diff_in_years = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "weeks"))/52.25
df1

So the resultant data frame will be

Difference of two dates in R by days, weeks, months, quarter, year 5

 

 

Get difference between two dates in R by quarter with an example:

Difference between two dates in R by quarter can be calculated using difftime function in roundabout way with argument units = “days” divided by (365.25/4) as shown below

# difference in quarter

df1$diff_in_quarter = as.numeric(difftime(df1$Date_of_joining, df1$Date_of_birth, units = "days"))/(365.25/4)
df1

So the resultant data frame will be

Difference of two dates in R by days, weeks, months, quarter, year 6

 

                                                                                                             

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts