From romain.francois at dbmail.com  Fri Nov 20 10:02:39 2009
From: romain.francois at dbmail.com (Romain Francois)
Date: Fri, 20 Nov 2009 10:02:39 +0100
Subject: [Rd] Surprising length() of POSIXlt vector (PR#14073)
In-Reply-To: <4B06594A.8030802@biostat.ku.dk>
References: <20091119223013.53B2C282EF57@mail.pubhealth.ku.dk>
	<4B06594A.8030802@biostat.ku.dk>
Message-ID: <4B065B2F.8040802@dbmail.com>

On 11/20/2009 09:54 AM, Peter Dalgaard wrote:
>
> mark at celos.net wrote:
>> Arrays of POSIXlt dates always return a length of 9. This
>> is correct (they're really lists of vectors of seconds,
>> hours, and so forth), but other methods disguise them as
>> flat vectors, giving superficially surprising behaviour:
>>
>> strings <- paste('2009-1-', 1:31, sep='')
>> dates <- strptime(strings, format="%Y-%m-%d")
>>
>> print(dates)
>> # [1] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05"
>> # [6] "2009-01-06" "2009-01-07" "2009-01-08" "2009-01-09" "2009-01-10"
>> # [11] "2009-01-11" "2009-01-12" "2009-01-13" "2009-01-14" "2009-01-15"
>> # [16] "2009-01-16" "2009-01-17" "2009-01-18" "2009-01-19" "2009-01-20"
>> # [21] "2009-01-21" "2009-01-22" "2009-01-23" "2009-01-24" "2009-01-25"
>> # [26] "2009-01-26" "2009-01-27" "2009-01-28" "2009-01-29" "2009-01-30"
>> # [31] "2009-01-31"
>>
>> print(length(dates))
>> # [1] 9
>> str(dates)
>> # POSIXlt[1:9], format: "2009-01-01" "2009-01-02" "2009-01-03"
>> "2009-01-04" ...
>>
>> print(dates[20])
>> # [1] "2009-01-20"
>>
>> print(length(dates[20]))
>> # [1] 9
>>
>> I've since realised that POSIXct makes date vectors easier,
>> but could we also have something like:
>>
>> length.POSIXlt <- function(x) { length(x$sec) }
>>
>> in datetime.R, to avoid breaking functions (like the
>> str.POSIXt method) which use length() in this way?
>
>
> [You need "wishlist" in the title for this sort of stuff.]
>
> I'd be wary of this. Just the other day we found that identical() broke
> on some objects because a package had length() redefined as a class
> method. I.e. the danger is that something wants to use length() with its
> original low-level interpretation.

str is another example

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/EAD5 : LondonR slides
|- http://tr.im/BcPw : celebrating R commit #50000
`- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc



[Rd] Surprising length() of POSIXlt vector (PR#14073)
Romain Francois