gpt4 book ai didi

r - "unlist"删除列表元素中的类 (POSIXlt)

转载 作者:行者123 更新时间:2023-12-04 22:44:59 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why does unlist() kill dates in R?

(2 个回答)


5年前关闭。




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

最佳答案

通常最好使用 POSIXct .但是,如果您的列表没有嵌套,您可以使用 c :

do.call(c, dates)

关于r - "unlist"删除列表元素中的类 (POSIXlt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com