[R] difftime problems with daylight saving time?

ravi rv15i at yahoo.se
Wed Sep 15 10:39:57 CEST 2010


Hi,
I seem to be having problems in calculating the period between two times, one 
with and the other without daylight saving time. Consider the following command 
:

> as.numeric(difftime(as.POSIXct("2010-04-01 06:00:00 
>CEST"),as.POSIXct("2010-03-03 08:27:00 CET"),units="hours"))
[1] 692.55

No problem here. Strangely enough, I seem to be having problems in using the 
same command when applied to a data frame. Let me explain with the following 
code :
df<-structure(list(d1 = structure(c(1263279420, 1263885000, 1264492200,
1265094120, 1265784900, 1266391860, 1266908220, 1267515240, 1268117220,
1268722020, 1269327240, 1269841740, 1270532400, 1271138400, 1271833380,
1272349740, 1272953820, 1273470060, 1274075640, 1274768100, 1275285060,
1275976500, 1276494480, 1277100300, 1277703480, 1278309420, 1278913620,
NA, NA, NA, NA, 1282198020, 1282630920, 1283149380), class = c("POSIXt",
"POSIXct"), tzone = ""), d2 = structure(c(1263367500, 1263972540,
1264578600, 1265182740, 1265872260, 1266479460, 1266996720, 1267601220,
1268204100, 1268811960, 1269415200, 1269929340, 1270620300, 1271224800,
1271918520, 1272436020, 1273045800, 1273558740, 1274163720, 1274854140,
1275373620, 1276062900, 1276583400, 1277187060, 1277792100, 1278397800,
1279002840, NA, NA, NA, NA, 1282285380, 1282718460, 1283237940
), class = c("POSIXt", "POSIXct"), tzone = "")), .Names = c("d1",
"d2"), class = "data.frame", row.names = c(NA, -34L))
# get the first of corresponding month for d1
g1<-as.POSIXlt(df$d1,format="%Y-%m-%d %H:%M:%S")
g1$mday<-1;g1$hour<-6;g1$min<-0;
df$m1<-as.POSIXct(g1)
# period between d1 and m1
df$nh1<-with(df,round(as.numeric(difftime(d1,m1,units="hours")),1))
# get the first of next month for d2
g1$mon<-g1$mon+1
df$m2<-g1
#period between m2 and d2
df$nh2<-with(df,round(as.numeric(difftime(m2,d2,units="hours")),1))
str(df)

I get the following result :
> df
                    d1                  d2                  m1   
nh1                  m2   nh2
1  2010-01-12 07:57:00 2010-01-13 08:25:00 2010-01-01 06:00:00 265.9 2010-02-01 
06:00:00 453.6
2  2010-01-19 08:10:00 2010-01-20 08:29:00 2010-01-01 06:00:00 434.2 2010-02-01 
06:00:00 285.5
3  2010-01-26 08:50:00 2010-01-27 08:50:00 2010-01-01 06:00:00 602.8 2010-02-01 
06:00:00 117.2
4  2010-02-02 08:02:00 2010-02-03 08:39:00 2010-02-01 06:00:00  26.0 2010-03-01 
06:00:00 621.4
5  2010-02-10 07:55:00 2010-02-11 08:11:00 2010-02-01 06:00:00 217.9 2010-03-01 
06:00:00 429.8
6  2010-02-17 08:31:00 2010-02-18 08:51:00 2010-02-01 06:00:00 386.5 2010-03-01 
06:00:00 261.1
7  2010-02-23 07:57:00 2010-02-24 08:32:00 2010-02-01 06:00:00 530.0 2010-03-01 
06:00:00 117.5
8  2010-03-02 08:34:00 2010-03-03 08:27:00 2010-03-01 06:00:00  26.6 2010-04-01 
06:00:00    NA
9  2010-03-09 07:47:00 2010-03-10 07:55:00 2010-03-01 06:00:00 193.8 2010-04-01 
06:00:00    NA
10 2010-03-16 07:47:00 2010-03-17 08:46:00 2010-03-01 06:00:00 361.8 2010-04-01 
06:00:00    NA
11 2010-03-23 07:54:00 2010-03-24 08:20:00 2010-03-01 06:00:00 529.9 2010-04-01 
06:00:00    NA
12 2010-03-29 07:49:00 2010-03-30 08:09:00                <NA>    NA 2010-04-01 
06:00:00  45.9
13 2010-04-06 07:40:00 2010-04-07 08:05:00 2010-04-01 06:00:00 121.7 2010-05-01 
06:00:00 573.9
14 2010-04-13 08:00:00 2010-04-14 08:00:00 2010-04-01 06:00:00 290.0 2010-05-01 
06:00:00 406.0
15 2010-04-21 09:03:00 2010-04-22 08:42:00 2010-04-01 06:00:00 483.1 2010-05-01 
06:00:00 213.3
16 2010-04-27 08:29:00 2010-04-28 08:27:00 2010-04-01 06:00:00 626.5 2010-05-01 
06:00:00  69.5
17 2010-05-04 08:17:00 2010-05-05 09:50:00 2010-05-01 06:00:00  74.3 2010-06-01 
06:00:00 644.2
18 2010-05-10 07:41:00 2010-05-11 08:19:00 2010-05-01 06:00:00 217.7 2010-06-01 
06:00:00 501.7
19 2010-05-17 07:54:00 2010-05-18 08:22:00 2010-05-01 06:00:00 385.9 2010-06-01 
06:00:00 333.6
20 2010-05-25 08:15:00 2010-05-26 08:09:00 2010-05-01 06:00:00 578.2 2010-06-01 
06:00:00 141.8
21 2010-05-31 07:51:00 2010-06-01 08:27:00 2010-05-01 06:00:00 721.9 2010-06-01 
06:00:00  -2.5
22 2010-06-08 07:55:00 2010-06-09 07:55:00 2010-06-01 06:00:00 169.9 2010-07-01 
06:00:00 526.1
23 2010-06-14 07:48:00 2010-06-15 08:30:00 2010-06-01 06:00:00 313.8 2010-07-01 
06:00:00 381.5
24 2010-06-21 08:05:00 2010-06-22 08:11:00 2010-06-01 06:00:00 482.1 2010-07-01 
06:00:00 213.8
25 2010-06-28 07:38:00 2010-06-29 08:15:00 2010-06-01 06:00:00 649.6 2010-07-01 
06:00:00  45.8
26 2010-07-05 07:57:00 2010-07-06 08:30:00 2010-07-01 06:00:00  98.0 2010-08-01 
06:00:00 621.5
27 2010-07-12 07:47:00 2010-07-13 08:34:00 2010-07-01 06:00:00 265.8 2010-08-01 
06:00:00 453.4
28                <NA>                <NA>                <NA>    
NA                <NA>    NA
29                <NA>                <NA>                <NA>    
NA                <NA>    NA
30                <NA>                <NA>                <NA>    
NA                <NA>    NA
31                <NA>                <NA>                <NA>    
NA                <NA>    NA
32 2010-08-19 08:07:00 2010-08-20 08:23:00 2010-08-01 06:00:00 434.1 2010-09-01 
06:00:00 285.6
33 2010-08-24 08:22:00 2010-08-25 08:41:00 2010-08-01 06:00:00 554.4 2010-09-01 
06:00:00 165.3
34 2010-08-30 08:23:00 2010-08-31 08:59:00 2010-08-01 06:00:00 698.4 2010-09-01 
06:00:00  21.0

I would like to know the following :
1. Why do I get a NA result for m1[12]?
2. Why do I get NA for df$nh2[8:11]? If I run  df2$nh2[8] as a seperate command 
(shown at the very beginning of this posting), I get a result alright.
Do these difficulties have anything to do with one time object being CET and the 
other CEST? If yes, is there any workaround?

Thanking you,
Ravi



More information about the R-help mailing list