[R] POSIXlt class and lapply

Ista Zahn |@t@z@hn @end|ng |rom gm@||@com
Fri Feb 15 14:55:15 CET 2019


As a practical matter, you can't treat POSIXlt as a list. The
documentation could be clearer about this.  ?DateTimeClasses says

"Class ‘"POSIXlt"’ is a named list of vectors", and then later, "Note
that the internal list structure is somewhat hidden, as many methods
(including ‘length(x)’, ‘print()’ and ‘str’) apply to the abstract
date-time vector, as for ‘"POSIXct"’."

In other words, "POSIXct" is internally a list, but you can't really
treat it as one. If you want to access the internal list structure
directly, unclass it for first. For example:

> x = as.POSIXlt(Sys.time() - 1:20
>
> length(x)
[1] 20
> length(unclass(x))
[1] 11
> str(x)
 POSIXlt[1:20], format: "2019-02-15 08:53:16" "2019-02-15 08:53:15"
"2019-02-15 08:53:14" ...
> str(unclass(x))
List of 11
 $ sec   : num [1:20] 16.9 15.9 14.9 13.9 12.9 ...
 $ min   : int [1:20] 53 53 53 53 53 53 53 53 53 53 ...
 $ hour  : int [1:20] 8 8 8 8 8 8 8 8 8 8 ...
 $ mday  : int [1:20] 15 15 15 15 15 15 15 15 15 15 ...
 $ mon   : int [1:20] 1 1 1 1 1 1 1 1 1 1 ...
 $ year  : int [1:20] 119 119 119 119 119 119 119 119 119 119 ...
 $ wday  : int [1:20] 5 5 5 5 5 5 5 5 5 5 ...
 $ yday  : int [1:20] 45 45 45 45 45 45 45 45 45 45 ...
 $ isdst : int [1:20] 0 0 0 0 0 0 0 0 0 0 ...
 $ zone  : chr [1:20] "EST" "EST" "EST" "EST" ...
 $ gmtoff: int [1:20] -18000 -18000 -18000 -18000 -18000 -18000 -18000
-18000 -18000 -18000 ...
 - attr(*, "tzone")= chr [1:3] "" "EST" "EDT"
> sapply(x, length)
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
> sapply(unclass(x), length)
   sec    min   hour   mday    mon   year   wday   yday  isdst   zone gmtoff
    20     20     20     20     20     20     20     20     20     20     20

Best,
Ista

On Fri, Feb 15, 2019 at 7:15 AM Newell, Paul
<paul.newell using metoffice.gov.uk> wrote:
>
> Many thanks Bill Dunlap.
>
> You are correct that `lapply` calls `as.list`, which I should have seen if I had looked a little harder.
>
> Whether that would have led me to locate `as.list.POSIXlt` is another matter.
>
> Best wishes.
>
>
>
> From: William Dunlap <wdunlap using tibco.com>
> Sent: 14 February 2019 20:03
> To: Newell, Paul
> Cc: r-help using r-project.org
> Subject: Re: [R] POSIXlt class and lapply
>
> Somewhere between R-3.3.3 and R-3.5.2 a POSIXlt method for as.list() was added, and lapply probably calls as.list().
>
>
> > RCompare(methods("as.list"))
> R version 3.3.3 (2017-03-06)                        | R version 3.5.1 (2018-07-02)
> [1] as.list.data.frame      as.list.Date            | [1] as.list.data.frame      as.list.Date
> [3] as.list.default         as.list.environment     | [3] as.list.default         as.list.environment
> [5] as.list.factor          as.list.function        | [5] as.list.factor          as.list.function
> [7] as.list.numeric_version as.list.POSIXct         | [7] as.list.numeric_version as.list.POSIXct
> see '?methods' for accessing help and source code   | [9] as.list.POSIXlt
>                                                     | see '?methods' for accessing help and source code
>
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
>
>
> On Thu, Feb 14, 2019 at 9:45 AM Newell, Paul <paul.newell using metoffice.gov.uk> wrote:
>
> Dear R-helpers,
>
> We have recently upgraded from R-3.3.1 to R-3.5.2.
>
> It seems there has been a change in behaviour of `lapply` and the `POSIXlt` class that I cannot find explicitly documented.
>
>
> In R-3.3.1:
>
> > lapply(as.POSIXlt(Sys.Date()), length)
> $sec
> [1] 1
> $min
> [1] 1
> $hour
> [1] 1
> $mday
> [1] 1
> $mon
> [1] 1
> $year
> [1] 1
> $wday
> [1] 1
> $yday
> [1] 1
> $isdst
> [1] 1
>
>
> whereas, in R-3.5.2:
>
> > lapply(as.POSIXlt(Sys.Date()), length)
> [[1]]
> [1] 1
>
>
> Is this change in behaviour intentional?
>
> Realistically, I cannot see anything documented to say that `lapply` should behave as per R-3.3.1 on a `POSIXlt` object, so it is/was perhaps unwise to rely on it.
>
>
> Best wishes,
> Paul Newell
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list