"unlist" 删除列表元素中的 class (POSIXlt)

"unlist" removes class in list elements (POSIXlt)

我有一个多级列表对象,其中所有元素都是 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"

当我尝试使用 unlist 制作单个矢量时,对象被转换为 numeric(即使用 unlist(dates))。如何维护 POSIXlt class?

示例数据:

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"))

通常使用 POSIXct 会好得多。但是,如果您的列表没有嵌套,您可以使用 c:

do.call(c, dates)

POSIXlt 对象本身就是列表,默认情况下 unlist 将递归取消列出所提供对象的所有子对象。我认为使用 unlist(dates, FALSE) 会起作用,但它不起作用。选项是:

使用do.callc:

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

通过POSIXct

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

但是请注意,需要将 y 的属性恢复到 unlist 丢失的属性。