我有一个多级列表的对象,其中所有元素都是类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类?
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"))
答案 0 :(得分:5)
POSIXlt
个对象本身就是列表,默认情况下unlist
会递归地取消列出所提供对象的所有子对象。我认为使用unlist(dates, FALSE)
会起作用,但事实并非如此。选项包括:
使用do.call
和c
:
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
丢失的NULL
。
答案 1 :(得分:4)
通常使用POSIXct
要好得多。但是,如果您的列表未嵌套,则可以使用c
:
do.call(c, dates)