Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unlist" removes class in list elements (POSIXlt) [duplicate]

Tags:

r

posixlt

I have an object that is a multi-level list, where all elements are of class POSIXlt:

[[1]]
 [1] "2015-07-16 GMT" "2015-07-16 GMT" "2015-07-16 GMT" "2015-07-16 GMT" "2015-07-15 GMT"
 [6] "2015-07-15 GMT" "2015-07-15 GMT" "2015-07-15 GMT" "2015-07-15 GMT" "2015-07-14 GMT"
[11] "2015-07-14 GMT" "2015-07-14 GMT" "2015-07-14 GMT" "2015-07-14 GMT" "2015-07-14 GMT"
[16] "2015-07-14 GMT" "2015-07-14 GMT" "2015-07-14 GMT" "2015-07-14 GMT" "2015-07-14 GMT"
[21] "2015-07-13 GMT" "2015-07-13 GMT" "2015-07-13 GMT" "2015-07-13 GMT" "2015-07-13 GMT"
[26] "2015-07-13 GMT" "2015-07-13 GMT" "2015-07-13 GMT" "2015-07-13 GMT"

[[2]]
 [1] "2015-07-12 GMT" "2015-07-11 GMT" "2015-07-11 GMT" "2015-07-11 GMT" "2015-07-11 GMT"
 [6] "2015-07-10 GMT" "2015-07-10 GMT" "2015-07-10 GMT" "2015-07-09 GMT" "2015-07-09 GMT"
[11] "2015-07-09 GMT" "2015-07-09 GMT" "2015-07-09 GMT" "2015-07-09 GMT" "2015-07-09 GMT"
[16] "2015-07-09 GMT" "2015-07-08 GMT" "2015-07-08 GMT" "2015-07-08 GMT" "2015-07-08 GMT"
[21] "2015-07-08 GMT" "2015-07-08 GMT" "2015-07-08 GMT" "2015-07-07 GMT" "2015-07-07 GMT"
[26] "2015-07-07 GMT" "2015-07-07 GMT" "2015-07-07 GMT" "2015-07-06 GMT"

[[3]]
 [1] "2015-07-06 GMT" "2015-07-06 GMT" "2015-07-06 GMT" "2015-07-06 GMT" "2015-07-06 GMT"
 [6] "2015-07-06 GMT" "2015-07-06 GMT" "2015-07-06 GMT" "2015-07-06 GMT" "2015-07-05 GMT"
[11] "2015-07-05 GMT" "2015-07-05 GMT" "2015-07-05 GMT" "2015-07-05 GMT" "2015-07-04 GMT"
[16] "2015-07-04 GMT" "2015-07-04 GMT" "2015-07-04 GMT" "2015-07-03 GMT" "2015-07-03 GMT"
[21] "2015-07-03 GMT" "2015-07-03 GMT" "2015-07-03 GMT" "2015-07-03 GMT" "2015-07-03 GMT"
[26] "2015-07-03 GMT" "2015-07-02 GMT" "2015-07-02 GMT" "2015-07-02 GMT"

When I try to make a single vector with unlist, the object is converted to numeric (i.e. with unlist(dates)). How can I maintain a POSIXlt class?

Example data:

dates <- list(structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(16L, 
16L, 16L, 16L, 15L, 15L, 15L, 15L, 15L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L), mon = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L), year = c(115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L), 
    wday = c(4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), yday = c(196L, 196L, 196L, 196L, 195L, 195L, 195L, 
    195L, 195L, 194L, 194L, 194L, 194L, 194L, 194L, 194L, 194L, 
    194L, 194L, 194L, 193L, 193L, 193L, 193L, 193L, 193L, 193L, 
    193L, 193L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("sec", "min", "hour", "mday", 
"mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt", 
"POSIXt"), tzone = "GMT"), structure(list(sec = c(0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), mday = c(12L, 11L, 11L, 11L, 11L, 10L, 10L, 10L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 
7L, 7L, 7L, 7L, 6L), mon = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L), year = c(115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L), wday = c(0L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
1L), yday = c(192L, 191L, 191L, 191L, 191L, 190L, 190L, 190L, 
189L, 189L, 189L, 189L, 189L, 189L, 189L, 189L, 188L, 188L, 188L, 
188L, 188L, 188L, 188L, 187L, 187L, 187L, 187L, 187L, 186L), 
    isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L)), .Names = c("sec", "min", "hour", "mday", "mon", 
"year", "wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt"
), tzone = "GMT"), structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), mday = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 
5L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L
), mon = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), year = c(115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L, 115L), wday = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 6L, 6L, 6L, 
6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L), yday = c(186L, 
186L, 186L, 186L, 186L, 186L, 186L, 186L, 186L, 185L, 185L, 185L, 
185L, 185L, 184L, 184L, 184L, 184L, 183L, 183L, 183L, 183L, 183L, 
183L, 183L, 183L, 182L, 182L, 182L), isdst = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec", "min", 
"hour", "mday", "mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt", 
"POSIXt"), tzone = "GMT"))
like image 434
Marc in the box Avatar asked Dec 20 '22 02:12

Marc in the box


2 Answers

Usually it's far better to use POSIXct. However, if your list is not nested you could use c:

do.call(c, dates)
like image 146
Roland Avatar answered Jan 15 '23 06:01

Roland


The POSIXlt objects are themselves lists, and by default unlist will recursively unlist all of the children of the provided object. I thought that using unlist(dates, FALSE) would work, but it doesn't. The options are:

Use do.call and c:

new_dates <- do.call("c", dates)

Go via POSIXct

x <- lapply(dates, as.POSIXct)
y <- unlist(x)
attributes(y) <- attributes(x[[1]])
new_dates <- as.POSIXlt(y)

Note however the need to restore the attributes to y which were lost by unlist.

like image 21
Nick Kennedy Avatar answered Jan 15 '23 06:01

Nick Kennedy