Search code examples
rdatespatialsatelliteadehabitathr

Converting my dates into a POSIXct class


I'm currently working my way through the adehabitatLT package. I've put my date_time column into characters and named it da:

da<-as.character(dat$date_time)

head(da)

[1] "7/08/2015 0:22" "7/08/2015 0:52" "7/08/2015 1:22" "7/08/2015 1:52" "7/08/2015 2:56" "7/08/2015 3:26"

As you can see my date_time input is a bit non traditional and i think this is where the error occurs, because when i create the class POSIXct:

da<-as.POSIXct(strptime(as.character(dat$date_time),"%d/%m/%y% H:%M:%S"))

It creates the class but i get NA for all my values:

head(da) [1] NA NA NA NA NA NA

My end objective here is to create an object of the class ltraj (but not only containing the date but the time as well).

Any ideas anyone?

Kind regards,

Sam

da<-as.POSIXct(strptime(as.character(locs$Date),"%y%m%d"))


Solution

  • While parsing tricky date/time formats, it might be useful to use lubridate package by Garrett Grolemund and Hadley Wickham.

    In your case, simply do

    require(lubridate)
    a <- dmy_hm(da)
    

    The separator and the number of digits for day or month or hours etc are automatically parsed.

    Find more info here