From murdoch at stats.uwo.ca  Wed Dec 12 15:43:02 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed, 12 Dec 2007 09:43:02 -0500
Subject: [Rd] Wrong length of POSIXt vectors (PR#10507)
In-Reply-To: <20071211112025.811162834156@mail.pubhealth.ku.dk>
References: <20071211112025.811162834156@mail.pubhealth.ku.dk>
Message-ID: <475FF376.8080806@stats.uwo.ca>

On 12/11/2007 6:20 AM, simecek at gmail.com wrote:
> Full_Name: Petr Simecek
> Version: 2.5.1, 2.6.1
> OS: Windows XP
> Submission from: (NULL) (195.113.231.2)
> 
> 
> Several times I have experienced that a length of a POSIXt vector has not been
> computed right.
> 
> Example:
> 
> tv<-structure(list(sec = c(50, 0, 55, 12, 2, 0, 37, NA, 17, 3, 31
> ), min = c(1L, 10L, 11L, 15L, 16L, 18L, 18L, NA, 20L, 22L, 22L
> ), hour = c(12L, 12L, 12L, 12L, 12L, 12L, 12L, NA, 12L, 12L, 
> 12L), mday = c(13L, 13L, 13L, 13L, 13L, 13L, 13L, NA, 13L, 13L, 
> 13L), mon = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, NA, 5L, 5L, 5L), year = c(105L, 
> 105L, 105L, 105L, 105L, 105L, 105L, NA, 105L, 105L, 105L), wday = c(1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, NA, 1L, 1L, 1L), yday = c(163L, 163L, 
> 163L, 163L, 163L, 163L, 163L, NA, 163L, 163L, 163L), isdst = c(1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L)), .Names = c("sec", 
> "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
> ), class = c("POSIXt", "POSIXlt"))
> 
> print(tv)
> # print 11 time points (right)
> 
> length(tv)
> # returns 9 (wrong)

tv is a list of length 9.  The answer is right, your expectation is wrong.
> 
> I have tried that on several computers with/without switching to English
> locales, i.e. Sys.setlocale("LC_TIME", "en"). I have searched a help pages but I
> cannot imagine how that could be OK.

See this in ?POSIXt:

Class '"POSIXlt"' is a named list of vectors...

You could define your own length measurement as

length.POSIXlt <- function(x) length(x$sec)

and you'll get the answer you expect, but be aware that length.XXX 
methods are quite rare, and you may surprise some of your users.

Duncan Murdoch



[Rd] Wrong length of POSIXt vectors (PR#10507)
Duncan Murdoch