Calculation of Difference between Dates

Asked

Viewed 488 times

4

I have the following DF:

MATRICULA <- c('111','222','333','444','555')
DATA_INICIO <- c('10/12/2017','31/12/2014', 
'30/06/2015','20/11/2016','01/04/2014')
DATA_FIM <- c('10/12/2017', '01/01/2015', '02/07/2016', '03/12/2016', 
'13/04/2014')

DADOS <- data.frame(MATRICULA,DATA_INICIO, DATA_FIM).

How to include a date difference column (in days) between the two dates (DATA_FIM (-) DATA_INICIO)?

3 answers

6

You can do it this way:

library(lubridate)
DADOS %>% 
   mutate(diferenca = as.numeric(dmy(DATA_FIM) - dmy(DATA_INICIO)))

  MATRICULA DATA_INICIO   DATA_FIM diferenca
1       111  10/12/2017 10/12/2017         0
2       222  31/12/2014 01/01/2015         1
3       333  30/06/2015 02/07/2016       368
4       444  20/11/2016 03/12/2016        13
5       555  01/04/2014 13/04/2014        12

Note that I use the function dmy of lubridate that converts strings to date according to the order they are in (d) , month (m) and year (y). If your date was in yyyy/mm/dd format you could use the function ymd for example.

5


The base R has functions to make calculations with dates, for simple cases such as difference in days (or other units) no need to load external packages.

DADOS$DIFERENÇA <- with(DADOS, as.Date(DATA_FIM, "%d/%m/%Y") - as.Date(DATA_INICIO, "%d/%m/%Y"))
DADOS
#  MATRICULA DATA_INICIO   DATA_FIM DIFERENÇA
#1       111  10/12/2017 10/12/2017    0 days
#2       222  31/12/2014 01/01/2015    1 days
#3       333  30/06/2015 02/07/2016  368 days
#4       444  20/11/2016 03/12/2016   13 days
#5       555  01/04/2014 13/04/2014   12 days

If you don’t want this kind of way out, with days in the column, just do

DADOS$DIFERENÇA <- as.integer(DADOS$DIFERENÇA)

0

R has a function called difftime, which calculates this difference in years, days, months and more.

First you will need to turn your data into date, then use this function.

DADOS$DATA_INICIO = as.Date(DADOS$DATA_INICIO, '%d/%m/%Y')
DADOS$DATA_FIM = as.Date(DADOS$DATA_INICIO, '%d/%m/%Y')

Then you can calculate according to your need and put in the same data.frame:

DADOS$INTERVALO = difftime(DADOS$DATA_FIM, DADOS$DATA_INICIO, units = 'days')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.