-2

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

SamR
  • 5
  • 3

2 Answers2

1

The format should be modified to

as.POSIXct(strptime(da, "%d/%m/%Y %H:%M"))

Or if month is first followed by day, then change it to "%m/%d/%Y %H:%M"

akrun
  • 874,273
  • 37
  • 540
  • 662
0

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

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
smaug
  • 846
  • 10
  • 26