i am trying to calculate KUD 50% and 95% for my penguin tracking data but have run into an error. My aim is to calculate the home range densities and then export the data as a polygon.
library(adehabitatHR)
library(sp)
library(readxl)
library(rgdal)
HRtracks<- as.data.frame(read_excel("allinterpolatedtracks.xlsx"))
# assign the correct columns as coordinates
coordinates(HRtracks)<-c("x","y")
proj4string(HRtracks)<- CRS("+init=epsg:3857")
#convert the coordinates to utm format
tracks.utm<-spTransform(HRtracks, CRS("+proj=utm +zone=60 +datum=WGS84"))
Error occurs after running this part of the script, tracks.utm[,X] indicates the column the data is sorted by, which is by individual tripID so it should create a range for each separate track.
colkud<-kernelUD(tracks.utm[,3],h="href", grid=1000,same4all=T)
The error:
Error: Can't subset columns that don't exist.
x Location 3 doesn't exist.
i There are only 1 column.
I suspect i am missing something in my script, however i am not an experienced user as of yet so hoping to get some advice.
The data:
tracks.utm <- dput(new("SpatialPointsDataFrame", data = structure(list(TripID = c(1,
1, 1, 1, 1, 1)), row.names = c(NA, -6L), class = c("data.frame")),
coords.nrs = numeric(0), coords = structure(c(165846.488217799,
165846.488227808, 165846.488167749, 165846.488257839, 165846.488237819,
165846.488718291, -19995889.0262206, -19995889.0261311, -19995889.0262007,
-19995889.0261311, -19995889.0260814, -19995889.0254053), .Dim = c(6L,
2L), .Dimnames = list(NULL, c("x", "y"))), bbox = structure(c(165846.488167749,
-19995889.0262206, 165846.488718291, -19995889.0254053), .Dim = c(2L,
2L), .Dimnames = list(c("x", "y"), c("min", "max"))), proj4string = new("CRS",
projargs = "+proj=utm +zone=60 +datum=WGS84 +units=m +no_defs")))
Cheers
Based on dput, you only have one column in your data. It is good practice to examine your data! If you look at str(tracks.utm@data)
the only column is TripID and you are attempting to specify the third column.
If you look at the functions help you will see the xy
argument that you are passing specifies:
An object inheriting the class SpatialPoints containing the x and y relocations of the animal. If xy inherits the class SpatialPointsDataFrame, it should contain only one column (factor) corresponding to the identity of the animals for each relocation.
This means that TripID should be the only column, be a factor and correspond to the individual animal id(s). Since it is expected that there is only a single column it does not need to be specified, only the sp object. However, apparently the need for a single column does not seem to be the case and you can have multiple columns but need to specify the column containing the unique animal ids, thus the column bracket index.
To ensure that your animal ids are a factor (per help), I would recommend coercing the appropriate column (eg., TripID) to a factor.
library(sp)
library(adehabitatHR)
tracks.utm <- dput(new("SpatialPointsDataFrame", data = structure(list(TripID = c(1,
1, 1, 1, 1, 1)), row.names = c(NA, -6L), class = c("data.frame")),
coords.nrs = numeric(0), coords = structure(c(165846.488217799,
165846.488227808, 165846.488167749, 165846.488257839, 165846.488237819,
165846.488718291, -19995889.0262206, -19995889.0261311, -19995889.0262007,
-19995889.0261311, -19995889.0260814, -19995889.0254053), .Dim = c(6L,
2L), .Dimnames = list(NULL, c("x", "y"))), bbox = structure(c(165846.488167749,
-19995889.0262206, 165846.488718291, -19995889.0254053), .Dim = c(2L,
2L), .Dimnames = list(c("x", "y"), c("min", "max"))), proj4string = new("CRS",
projargs = "+proj=utm +zone=60 +datum=WGS84 +units=m +no_defs")))
tracks.utm@data$TripID <- factor(tracks.utm@data$TripID)
str(tracks.utm@data)
( colkud <- adehabitatHR::kernelUD(tracks.utm[,1], h="href",
grid=1000, same4all=TRUE) )
image(colkud)
points(tracks.utm, pch=20)