Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the units of difftime after computing the values, and not using units="xxx" when performing the calculations

Tags:

date

r

I am substracting dates. In the following example

bb

FECHA_EFECTO_ESTADO FECHA_ANIVERSARIO_POLIZA
9  2015-11-05 09:49:00               2015-11-05
10 2015-11-05 09:51:00               2015-11-04

the columns are posixct date values. To create a new variable which is the difference of the other variables I could use:

library (dplyr)
bb<-aa<-mutate(bb, day1=abs(FECHA_EFECTO_ESTADO-FECHA_ANIVERSARIO_POLIZA))
bb
  FECHA_EFECTO_ESTADO FECHA_ANIVERSARIO_POLIZA            day1
1 2015-11-05 09:49:00               2015-11-05  9.816667 hours
2 2015-11-05 09:51:00               2015-11-04 33.850000 hours

By default, the units (days, hours, seconds) if the day1 variable depends on the amount of the difference. If I want to have the difference in days, I could do:

bb<-mutate(bb, day2=abs (difftime(FECHA_EFECTO_ESTADO, FECHA_ANIVERSARIO_POLIZA, units="days" )))
bb
  FECHA_EFECTO_ESTADO FECHA_ANIVERSARIO_POLIZA            day1           day2
1 2015-11-05 09:49:00               2015-11-05  9.816667 hours 0.4090278 days
2 2015-11-05 09:51:00               2015-11-04 33.850000 hours 1.4104167 days

IS there a way to specify the units (days in this case) after doing the calculations? I might find further down the analysis that I would prefer to have the difference in hours for instance, so:
How can I change the units of the day1 or day2 columns a posteriori?
Thanks

like image 866
Miguel Rayon Gonzalez Avatar asked Dec 07 '15 14:12

Miguel Rayon Gonzalez


2 Answers

What is returned by difftime is an object of class "difftime".

Other function have methods for difftime. For example, to convert to the number of hours, as a numeric:

as.numeric(difftime("2015-12-07", "2015-12-05"), units="hours") 

[1] 48

Or, to get weeks:

as.numeric(difftime("2015-12-07", "2015-12-05"), units="weeks") 

[1] 0.2857143

Also, you might find it useful to remember that POSIXct objects are actually numbers! They represent the number of seconds elapsed since ‘1970-01-01 00:00.00 UTC’, (assumes Gregorian Calendar).

As a result, it can often be convenient to think of a unit of time of fixed duration (i.e., not something like "month", which isn't constant) and perform calculations using it. E.g., once your time difference is in seconds, it's to convert to other constant (technically there is a very fine scale variation in some of these, but most applications don't require attention to these details) time units like minutes, hours, days, or weeks.

like image 104
rbatt Avatar answered Oct 14 '22 16:10

rbatt


Now, thanks to @rbatt I have realised that, in my example, to change the units of variable day1, I can do bb$day1<- as.numeric(bb$day1, units="days")

And this will change the units to days:

FECHA_EFECTO_ESTADO FECHA_ANIVERSARIO_POLIZA            day1           day2
9  2015-11-05 09:49:00               2015-11-05  9.816667 hours 0.4090278 days
10 2015-11-05 09:51:00               2015-11-04 33.850000 hours 1.4104167 days

bb$day1<- as.numeric(bb$day1, units="days")

bb

   FECHA_EFECTO_ESTADO FECHA_ANIVERSARIO_POLIZA      day1           day2
9  2015-11-05 09:49:00               2015-11-05 0.4090278 0.4090278 days
10 2015-11-05 09:51:00               2015-11-04 1.4104167 1.4104167 days
like image 44
Miguel Rayon Gonzalez Avatar answered Oct 14 '22 17:10

Miguel Rayon Gonzalez