Gabor == Gabor Grothendieck [EMAIL PROTECTED]
on Sun, 23 Jul 2006 09:02:35 -0400 writes:
Gabor Moving this to r-devel.
[would have been a good idea ... but you didn't;
I think it's too late now; rather keep the msg thread together]
Gabor Looking at the diff.POSIXt
Gabor code
Dear Listers,
I have encountered a strange problem using diff() and POSIXt:
dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006)
dts - strptime(dts, %d/%m/%Y)
class(dts)
[1] POSIXt POSIXlt
diff(dts)
Time differences of 7862400,
Try converting to POSIXct:
str(dts)
'POSIXlt', format: chr [1:10] 2003-04-15 2003-07-15 2003-10-15
2004-04-15 2004-07-15 2004-10-15 2005-04-15 ...
dts
[1] 2003-04-15 2003-07-15 2003-10-15 2004-04-15 2004-07-15
2004-10-15 2005-04-15 2005-07-15
[9] 2005-10-15 2006-04-15
dts - as.POSIXct(dts)
Try converting to POSIXct:
That's what I did finally (see the previous e-mail).
dts-c(15/4/2003,15/7/2003,15/10/2003,15/04/2004,15/07/2004,15/10/2004,15/4/2005,15/07/2005,15/10/2005,15/4/2006)
dts - as.POSIXct(strptime(dts, %d/%m/%Y))
diff(dts)
Time differences of 91, 92, 183, 91, 92,
Moving this to r-devel.
Looking at the diff.POSIXt code we see the problem is that it takes the
length of the input using length which is wrong since in the case
of POSIXlt the length is always 9 (or maybe length should be
defined differently for POSIXlt?). Try this which gives the same
problem:
Hi, Gabor:
For my 0.02 euros, I vote to make length(POSIXlt) = length of the
series, NOT the length of the list = 9 always. I've stubbed my toe on
that one many times. I always fix it by converting first to POSIXct.
The key question is what would users naively expect to
Just one more comment. It is possible to define length.POSIXlt yourself
in which case diff works with POSIXlt objects.
length.POSIXlt - function(x) length(x[[1]])
diff(dts)
Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days
On 7/23/06, Gabor Grothendieck [EMAIL PROTECTED]
OK. Got it. Thanks a lot everybody.
I however feel that although the problem can be technically handled by
any user aware of it, it should be fixed in R in a more general way,
either by modifying the diff() code so that it really handles any kind
of POSIXt (POSIXlt and POSIXct) with the same