0

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")
Kevin
  • 229
  • 3
  • 9
  • 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). – r2evans Aug 28 '23 at 20:38

1 Answers1

1

Your date object is not of class POSIXct, it is a data.frame. Don't mistake a frame with one column (that happens to be POSIXct) equivalent for functions as a POSIXct vector.

Also, I see no reason to break it out into individual vectors, that leaves room for inconsistent indexing, etc. I suggest something like this:

elk <- data %>%
  mutate(date = as.POSIXct(Date_Time_MST, format = "%m/%d/%Y", tz  ="US/Mountain"))) %>%
  mutate(AID = as.character(AID))

tr <- with(elk[elk$AID == "17",],
           as.ltraj(xy = coordinates(cbind(Easting, Northing), date = date, id = AID))

If you're going to be doing this one-call for multiple ids (or even all of them) in elk and want to automate it, you can generate a list-column (saved for later use) with something like:

elk_with_tr <- elk %>%
  filter(id %in% c("17", ...)) %>%        # some other IDs of interest to you
  mutate(
    tr = Map(as.ltraj, xy=coordinates(cbind(Easting, Northing)), date=date, id=AID)
  )

and this new frame will have a list-column named tr with all of the results.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Thanks, this was helpful. I ended up running into other errors though (e.g., xy does not exist in elk) but ultimately I think I sorted it out...elk1<-elk%>% dplyr::select(AID,Easting, Northing, date)%>% distinct()%>% nest(data=-AID)%>% mutate(traj=map2(AID,data,~as.ltraj(xy=data.frame(x=.y$Easting, y=.y$Northing),date=.y$date,id=.x))) – Kevin Aug 28 '23 at 21:46
  • 1
    Oops, right, your `xy` was two columns, I hastened over that and did it wrong ... editing for clarity, sorry about that ... – r2evans Aug 28 '23 at 21:59