Search code examples
rdateposixlt

How to convert list of dates to POSIXlt objects?


I want to run the bfastts function (https://www.rdocumentation.org/packages/bfast/versions/1.5.7/topics/bfastts) on a series of dates formatted as dd-mm-yyyy and a series of values to convert them into a time series. For this function the dates need to be of "POSIXlt" type. However, when running the code

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

I am getting the error

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

When running

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

The returned list seems to be empty. When inputting my date list without conversion into the bfastts I'm getting the error

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

I am not used to coding in R as I usually work in python. I've tried googling all three errors but I can't find a solution. Could any one provide me some pointers?

Edit:

dput(dates) gives me:

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

Solution

  • The problem is that dates is a list, but you actually want to access the first entry (V1) of it. Further you have to specify that the dates you are providing are in the format dd-mm-yyyy. This you can do with format = "%d-%m-%Y". Thus the following works:

    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" 
    # ...
    

    Data

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