Reputation: 673
I was trying to get a time difference col2-col1 for a data frame in R, using lubridate. This is the format that I have:
V1 V2
1 2014-07-01 19:15:03 2014-07-01 19:19:44
2 2014-07-01 19:29:57 2014-07-01 19:34:39
3 2014-07-01 19:44:53 2014-07-01 19:49:34
...
28 2014-07-02 02:28:25 2014-07-02 02:33:07
29 2014-07-02 02:43:20 2014-07-02 02:47:59
30 2014-07-02 02:58:12 2014-07-02 03:02:54
(pastebin link here http://pastebin.com/GfKJKqcV)
I try this
> lala <- hour(inicioyfin_n37_all$V2) + minute(inicioyfin_n37_all$V2)/60 + second(inicioyfin_n37_all$V2)/3600
> lolo <- hour(inicioyfin_n37_all$V1) + minute(inicioyfin_n37_all$V1)/60 + second(inicioyfin_n37_all$V1)/3600
> lala -lolo
[1] 0.07805556 0.07833333 0.07805556 0.07805556 0.07861111
[6] 0.07750000 0.07805556 0.07833333 0.07833333 0.07805556
[11] 0.07833333 0.07777778 0.07777778 0.07805556 0.07861111
[16] 0.07805556 0.07805556 0.07777778 -23.92194444 0.07833333
[21] 0.07750000 0.07777778 0.07805556 0.07750000 0.07777778
[26] 0.07805556 0.07805556 0.07833333 0.07750000 0.07833333
...but as you can see element number 19 is not really ok.
Even if I add the day value
> lala <- day(inicioyfin_n37_all$V2) + hour(inicioyfin_n37_all$V2) + minute(inicioyfin_n37_all$V2)/60 + second(inicioyfin_n37_all$V2)/3600
> lolo <- day(inicioyfin_n37_all$V1) + hour(inicioyfin_n37_all$V1) + minute(inicioyfin_n37_all$V1)/60 + second(inicioyfin_n37_all$V1)/3600
> lala -lolo
[1] 0.07805556 0.07833333 0.07805556 0.07805556 0.07861111 0.07750000 0.07805556
[8] 0.07833333 0.07833333 0.07805556 0.07833333 0.07777778 0.07777778 0.07805556
[15] 0.07861111 0.07805556 0.07805556 0.07777778 -22.92194444 0.07833333 0.07750000
[22] 0.07777778 0.07805556 0.07750000 0.07777778 0.07805556 0.07805556 0.07833333
[29] 0.07750000 0.07833333
Any suggestions? I am almost there... :(
Upvotes: 0
Views: 68
Reputation: 43364
You can use lubridate
's int_length
to calculate the length of an interval
, which is what you really have. It calculates the time in seconds, so divide as necessary to change units:
int_length(interval(df$V1, df$V2))
returns
[1] 281 282 281 281 283 279 281 282 282 281 282 280 280 281 283 281 281 280 281 282 279 280 281
[24] 279 280 281 281 282 279 282
Upvotes: 1