如何将日期列表转换为 POSIXlt 对象?

How to convert list of dates to POSIXlt objects?

我想 运行 bfastts 函数 (https://www.rdocumentation.org/packages/bfast/versions/1.5.7/topics/bfastts) 将一系列格式为 dd-mm-yyyy 的日期和一系列值转换为时间序列。对于此函数,日期需要是 "POSIXlt" 类型。然而,当运行宁代码

dv<-as.POSIXct.POSIXlt(dates, tz="")

我遇到错误

Error in as.POSIXct.POSIXlt(dates, tz = "") : invalid 'x' argument

当运行宁

dv<-strftime(as.POSIXct.POSIXlt(dates, tz="")

返回的列表似乎是空的。在输入我的日期列表而不转换为 bfastts 时,我收到错误

Error in as.POSIXlt.default(dates) : 
do not know how to convert 'dates' to class “POSIXlt”

我不习惯用 R 编码,因为我通常在 python 中工作。我尝试用谷歌搜索所有三个错误,但找不到解决方案。谁能给我一些指点?

编辑:

dput(dates) 给我:

list(V1 = structure(c(19L, 57L, 31L, 59L, 33L, 34L, 4L, 7L, 40L, 
12L, 50L, 56L, 3L, 37L, 6L, 39L, 46L, 17L, 43L, 55L, 30L, 2L, 
36L, 38L, 11L, 21L, 49L, 24L, 27L, 10L, 45L, 14L, 1L, 18L, 47L, 
54L, 29L, 32L, 8L, 42L, 9L, 16L, 44L, 48L, 23L, 51L, 52L, 26L, 
35L, 5L, 15L, 20L, 22L, 25L, 53L, 28L, 58L, 41L, 13L), .Label = c("1-8- 
2016", "11-5-2015", "11-7-2014", "12-10-2013", "12-2-2018", "12-8-2014", 
"13-11-2013", "13-3-2017", "14-4-2017", "14-6-2016", "14-7-2015", 
"15-12-2013", "15-2-2019", "16-7-2016", "17-4-2018", "17-6-2017", 
"18-12-2014", "18-9-2016", "19-4-2013", "19-5-2018", "2-10-2015", 
"20-6-2018", "20-8-2017", "21-12-2015", "22-7-2018", "23-10-2017", 
"23-2-2016", "23-8-2018", "24-1-2017", "24-3-2015", "24-7-2013", 
"25-2-2017", "25-8-2013", "26-9-2013", "27-1-2018", "27-5-2015", 
"27-7-2014", "28-6-2015", "28-8-2014", "29-11-2013", "29-12-2018", 
"29-3-2017", "3-1-2015", "3-7-2017", "30-6-2016", "31-10-2014", 
"4-10-2016", "4-8-2017", "5-12-2015", "5-3-2014", "5-9-2017", 
"7-10-2017", "7-8-2018", "8-1-2017", "8-3-2015", "8-5-2014", 
"8-7-2013", "8-9-2018", "9-8-2013"), class = "factor"))

问题是 dates 是一个列表,但您实际上想要访问它的第一个条目 (V1)。此外,您必须指定您提供的日期采用 dd-mm-yyyy 格式。这可以用 format = "%d-%m-%Y" 来完成。因此以下工作:

as.POSIXlt(dates$V1, format = "%d-%m-%Y", tz="")
# [1] "2013-04-19 CEST" "2013-07-08 CEST" "2013-07-24 CEST" "2013-08-09 CEST" 
# ...

数据

dates <- list(V1 = structure(c(19L, 57L, 31L, 59L, 33L, 34L, 4L, 7L, 40L, 
                      12L, 50L, 56L, 3L, 37L, 6L, 39L, 46L, 17L, 43L, 55L, 30L, 2L, 
                      36L, 38L, 11L, 21L, 49L, 24L, 27L, 10L, 45L, 14L, 1L, 18L, 47L, 
                      54L, 29L, 32L, 8L, 42L, 9L, 16L, 44L, 48L, 23L, 51L, 52L, 26L, 
                      35L, 5L, 15L, 20L, 22L, 25L, 53L, 28L, 58L, 41L, 13L), 
                    .Label = c("1-8-2016", "11-5-2015", "11-7-2014", "12-10-2013", "12-2-2018", "12-8-2014", 
                               "13-11-2013", "13-3-2017", "14-4-2017", "14-6-2016", "14-7-2015", 
                               "15-12-2013", "15-2-2019", "16-7-2016", "17-4-2018", "17-6-2017", 
                               "18-12-2014", "18-9-2016", "19-4-2013", "19-5-2018", "2-10-2015", 
                               "20-6-2018", "20-8-2017", "21-12-2015", "22-7-2018", "23-10-2017", 
                               "23-2-2016", "23-8-2018", "24-1-2017", "24-3-2015", "24-7-2013", 
                               "25-2-2017", "25-8-2013", "26-9-2013", "27-1-2018", "27-5-2015", 
                               "27-7-2014", "28-6-2015", "28-8-2014", "29-11-2013", "29-12-2018", 
                               "29-3-2017", "3-1-2015", "3-7-2017", "30-6-2016", "31-10-2014", 
                               "4-10-2016", "4-8-2017", "5-12-2015", "5-3-2014", "5-9-2017", 
                               "7-10-2017", "7-8-2018", "8-1-2017", "8-3-2015", "8-5-2014", 
                               "8-7-2013", "8-9-2018", "9-8-2013"), class = "factor"))