我正试图为我的企鹅追踪数据计算50%和95%的KUD,但遇到了一个错误。我的目标是计算主范围密度,然后将数据导出为多边形。
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"))
运行脚本的这一部分tracks后会出现错误。utm[,X]指示数据排序的列,该列是按单个tripID排序的,因此它应该为每个单独的track创建一个范围。
colkud<-kernelUD(tracks.utm[,3],h="href", grid=1000,same4all=T)
错误:
Error: Can't subset columns that don't exist.
x Location 3 doesn't exist.
i There are only 1 column.
我怀疑我的脚本中遗漏了一些东西,但我还不是一个经验丰富的用户,所以希望能得到一些建议。
数据:
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")))
干杯
基于dput,数据中只有一列。检查您的数据是一种很好的做法!如果您查看str(tracks.utm@data)
,那么唯一的列是TripID,并且您正试图指定第三列。
如果您查看函数帮助,您将看到正在传递的xy
参数指定:
一个继承类SpatialPoints的对象,该类包含x和y动物的迁移。如果xy继承了类SpatialPointsDataFrame,它应该只包含一列(因子(对应于每次重新定位的动物的身份。
这意味着TripID应该是唯一的列,是一个因子,并对应于单个动物id。由于预计只有一列,因此不需要指定,只需要指定sp对象。然而,显然不需要单独的列,您可以有多个列,但需要指定包含唯一动物ID的列,从而指定列括号索引。
为了确保你的动物ID是一个因素(根据帮助(,我建议将适当的列(例如,TripID(强制为一个因素。
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)