Error from adehabitatLT::as.ltraj() because dates are not class POSIXct but str(dates) indicates they are POSIXct

The adehabitatLR::as.ltraj() function calculates animal trajectories. The function requires dates to be class POSIXct. I followed the same steps in the example section of the help document using a play dataset I found online and converted date-time to POSIXct dates but I still get the following error when running the function

Error in adehabitatLT::as.ltraj(xy = xy[id == “17”, ], date = date[id
== : For objects of type II, date should be of class “POSIXct”

Here is what I’m doing…

library(tidyverse)
library(lubridate)
library(rio)
library(sp)
library(adehabitatLT)

#import elk collar data
data<-import("https://www.sciencebase.gov/catalog/file/get/59d90ceee4b05fe04cc9b0f2?f=__disk__a8%2F84%2Fa6%2Fa884a68596e8ed85b6b956119db0f1fffc4e960c")
glimpse(data)
#following example: convert date-time to posixct, and ID to character
elk<-data%>%mutate(date=as.POSIXct(strptime(Date_Time_MST,"%m/%d/%Y",tz="US/Mountain")))%>%mutate(AID=as.character(AID))
#get id vector
id<-elk%>%dplyr::select(AID)
#get xy data
xy<-elk%>%dplyr::select(Easting, Northing)%>%coordinates()%>%as.data.frame()
#get dates
date<-elk%>%dplyr::select(date)%>%as.data.frame()
str(date)
#get trajectories for AID 17
tr<-adehabitatLT::as.ltraj(xy=xy[id=="17",],date=date[id=="17"], id="17")

  • Your date is a dataframe, don’t mistake a frame with one column of POSIXct with a vector of class POSIXct, they are not equivalent. I don’t know why you’re breaking things out of your frame, I’d likely do something like tr <- with(elk[elk$id=="17",], as.ltraj(xy=xy, date=date, id="17")) (or similar … I don’t have that package either but the premise should work).

    – 




Leave a Comment