r语言 - 将spatialpixeldataframe转换为estUD



简而言之:我有一个跨网格(udspdf)的动物利用率分布的空间像素数据帧,我需要将其转换为estu -class。

我如何到达那里的背景:在计算了单个动物的KDE之后,我需要将estu -class对象转换为一个spatialpixeldataframe,以便删除非栖息地细胞,并将栖息地细胞的利用率分配重新调整为1。然后,我需要将spatialpixeldataframe转换回estUD类文件,以便可以在其上运行kerneloverlaphr()。

我有下面的代码,它将其转换为一个esstudm类。但是我需要它在esl -class,因为只有一种动物。

re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so@h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so@vol <- FALSE
return(so)
})
names(re) <- names(udspdf)
class(re) <- "estUDm"
image(re)

如果我改变

class(re) <- "estUD"

这似乎工作,但后来我可以看到有一个问题,因为

image(re)

显示以下错误:is(x, "GridTopology")中的错误:试图获得插槽"网格"从一个不是S4对象的对象(类"estUD")

我很抱歉,我不知道如何为这样一个例子提供一个可重复的例子,因为数据相当复杂。我希望有一个通用的代码。

任何提示感谢!

> str(re)
List of 1
$ ud:Formal class 'estUD' [package "adehabitatHR"] with 9 slots
.. ..@ h          :List of 2
.. .. ..$ h   : num 0
.. .. ..$ meth: chr "specified"
.. ..@ vol        : logi FALSE
.. ..@ data       :'data.frame':  4400000 obs. of  1 variable:
.. .. ..$ ud: num [1:4400000] 0 0 0 0 0 0 0 0 0 0 ...
.. ..@ coords.nrs : num(0) 
.. ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
.. .. .. ..@ cellcentre.offset: Named num [1:2] -70 -60
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. .. .. ..@ cellsize         : Named num [1:2] 0.01 0.01
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. .. .. ..@ cells.dim        : Named int [1:2] 2000 2200
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. ..@ grid.index : int [1:4400000] 1 2 3 4 5 6 7 8 9 10 ...
.. ..@ coords     : num [1:4400000, 1:2] -70 -70 -70 -70 -70 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:2] "Var2" "Var1"
.. ..@ bbox       : num [1:2, 1:2] -70 -60 -50 -38
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "Var2" "Var1"
.. .. .. ..$ : chr [1:2] "min" "max"
.. ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
- attr(*, "class")= chr "estUDm"

我的kerneloverlaphr()代码,它直接与kernelUD() (estUD-class)的输出一起工作,如下所示:

NWI15b, BCI15b和BCI15i是我的个体动物


library(adehabitatHR)

# convert list of KDEs (estUDs) to class estUDm
tot <- list(NWI15b=NWI15b, BCI15b=BCI15b, BCI15i=BCI15i)
class(tot) <- "estUDm"

#calculate overlap using kerneloverlaphr
kerneloverlaphr(tot, method = c("BA"), percent = 95, conditional = FALSE)
dput(head(as.data.frame.estUD(re), n = 10))
Error in h(simpleError(msg, call)) : 
error in evaluating the argument 'x' in selecting a method for function 'head': no method or default for coercing “estUDm” to “SpatialPixelsDataFrame”
> unique(re@data$ud)
Error in h(simpleError(msg, call)) : 
error in evaluating the argument 'x' in selecting a method for function 'unique': trying to get slot "data" from an object (class "estUDm") that is not an S4 object 

给kernel的对象结构


str(track_sp)

Formal class 'SpatialPoints' [package "sp"] with 3 slots
..@ coords     : num [1:9790, 1:2] -59.2 -59.2 -59.2 -59.2 -59.2 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:9790] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "x" "y"
..@ bbox       : num [1:2, 1:2] -65.8 -55.6 -56.1 -52.2
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"

我们有

re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so@h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so@vol <- FALSE
return(so)
})
> names(re) <- names(udspdf)
> class(re) <- "estUDm"
> image(re)

然后我们需要简单地加上:

colonyA <- re[[1]]
#and then do the same after running the code for the next colony
colonyB<- re[[1]]

最新更新