I started off with a SpatialPointsDataFrame, containing locations from animals wearing GPS loggers. Aside from the coordinates, the data frame also contains the animals' ID and a timestamp.
From this SpatialPointsDataFrame, I created lines that show the trajectory of each animal over the tracking period. I've used the following code:
spdf4tri
#the SpatialPointsDataFrame
ids=unique(spdf4tri$id)
trajectory <- list()
for (i in ids){
spdf4bird<-subset(spdf4tri, id==i)
birdtrajectory<-SpatialLines(list(Lines(list(Line(spdf4bird)), "id")))
trajectory[[i]]<-birdtrajectory
print(i)
}
Now, I have a problem, because trajectory
is of the class 'list'. I cannot figure out how to unlist it.
Best Answer
As I mentioned in my comment, you can use do.call for this. Here is an example.
First, add sp library and create a list with three SpatialLines objects (would work with Lines object as well).
Now, use do.call with rbind to merge the lines into a single SpatialLines object.