Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an R object of class "POSIXlt" a "list" or not?

Tags:

list

r

By the documentation of the class POSIXlt an object of class POSIXlt is a named list. And indeed:

> tm <- strptime( "24-12-2015 05:28:12", format="%d-%m-%Y %H:%M:%S", tz="UTC" )
> class(tm)
[1] "POSIXlt" "POSIXt" 

> tm$sec
[1] 12

> tm$min
[1] 28

> tm$hour
[1] 5

> tm$mday
[1] 24

> tm$mon
[1] 11

> tm$year
[1] 115

> tm$wday
[1] 4

> tm$yday
[1] 357

> tm$isdat
NULL

> tm$zone
NULL

> tm$gmtoff
NULL

The documentation of the class list says that is.list(tm) is TRUE if and only if tm is a list or a pairlist, and is.pairlist(tm) is TRUE if and only if tm is a pairlist or NULL.

> is.list(tm)
[1] TRUE
> is.pairlist(tm)
[1] FALSE

Hence tm must be a list.

But "list" is not a superclass of "POSIXlt":

> is(tm)
[1] "POSIXlt"  "POSIXt"   "oldClass"
> extends("POSIXlt")
[1] "POSIXlt"  "POSIXt"   "oldClass"

extends negates the the question if "POSIXlt" extents "list", the answer is not even "maybe":

> extends("POSIXlt","list")
[1] FALSE

Furthermore,

> is("POSIXlt","list")
[1] FALSE
> is(tm,"list")
[1] FALSE

By the documentation of is this means that tm cannot be treated as from "list". In particular tm is not a list. But if tm cannot be treated as from "list", why does as succeed in coercing tm to a list? as(tm.list is doubtless a list, whereas as.list(tm) and tm are identical:

> as(tm,"list")
[[1]]
[1] 12

[[2]]
[1] 28

[[3]]
[1] 5

[[4]]
[1] 24

[[5]]
[1] 11

[[6]]
[1] 115

[[7]]
[1] 4

[[8]]
[1] 357

[[9]]
[1] 0

> class(as(tm,"list"))
[1] "list"
> is.list(as(tm,"list"))
[1] TRUE
> is(as(tm,"list"),"list")
[1] TRUE
> identical(tm,as.list(tm))
[1] TRUE

as(tm,"list") does have the components specified in the documentation of the class POSIXlt, but the names are gone.

What does it mean to be a list? Is tm a list or not?

like image 714
mra68 Avatar asked Dec 24 '15 15:12

mra68


People also ask

What is as POSIXlt in R?

POSIXct() function in R is used to convert the character type setting default for UTC and 1970 to a POSIXct object.

What is the difference between POSIXct and POSIXt?

There are two POSIX date/time classes, which differ in the way that the values are stored internally. The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.

What is Posixt?

There are two basic classes of date/times. Class "POSIXct" represents the (signed) number of seconds since the beginning of 1970 as a numeric vector. Class "POSIXlt" is a named list of vectors representing sec 0–61: seconds min 0–59: minutes hour 0–23: hours mday 1–31: day of the month mon.


1 Answers

It is a named list with a c("POSIXct", "POSIXt") class and a tzone attribute:

POSIXlt = Named list + class + tzone attribute

In fact, we can build up or manufacture such an object from a named list L by adding the class and tzone attribute like this:

L <- list(sec = 12, min = 28L, hour = 5L, mday = 24L, mon = 11L, 
    year = 115L, wday = 4L, yday = 357L, isdst = 0L)

tm0 <- L # start with list L
class(tm0) <- c("POSIXlt", "POSIXt")  # add class
attr(tm0, "tzone") <- "UTC"  # add tzone

tm <- strptime( "24-12-2015 05:28:12", format="%d-%m-%Y %H:%M:%S", tz="UTC" )
identical(tm0, tm)
## [1] TRUE

We can recover the named list L from tm by removing the class and the tzone attribute:

tm <- strptime( "24-12-2015 05:28:12", format="%d-%m-%Y %H:%M:%S", tz="UTC" )  # start w tm
L0 <- unclass(tm)  # remove class
attr(L0, "tzone") <- NULL  # remove tzone
identical(L0, L)
## [1] TRUE
like image 151
G. Grothendieck Avatar answered Oct 01 '22 13:10

G. Grothendieck