Calculation of Difference between Dates

6

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 do I include a date difference column (in days) between the two dates ( DATA_FIM (-) DATA_INICIO )?

    
asked by anonymous 26.12.2018 / 15:45

2 answers

7

OR base has functions to do calculations with dates, for simple cases such as difference in days (or other drives) it is not necessary 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 do not want this type of output, with days in the column, just do

DADOS$DIFERENÇA <- as.integer(DADOS$DIFERENÇA)
    
26.12.2018 / 16:13
7

You can do this as follows:

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 dmy function of lubridate which converts strings to date according to the order they are day ( d ), month ( m ) and year ( y ). If your date was in the format yyyy / mm / dd you could use the ymd function for example.

    
26.12.2018 / 16:01