[英]Nearest-neighbour distances of a point pattern by year
我希望使用spatstat
按年份計算點模式的最近鄰距離 (NND),但出現錯誤,盡管我看不出與文檔中提供的示例有任何區別。 正確的做法是什么?
library(spatstat)
#Build point pattern
pp <- structure(list(window = structure(list(type = "rectangle", xrange = c(952727.038715708,
969663.326063713), yrange = c(1928725.27732809, 1943334.46685032
), units = structure(list(singular = "unit", plural = "units",
multiplier = 1), class = "unitname")), class = "owin"), n = 6L,
x = c(959238.044802669, 968582.344942023, 960218.207335433,
969663.326063713, 964665.091671559, 952727.038715708), y = c(1941763.77812852,
1934201.29833662, 1943334.46685032, 1939411.6354699, 1928725.27732809,
1931868.51667007), markformat = "vector", marks = c("1994",
"1994", "2005", "2005", "2005", "2005")), class = "ppp")
#Calculate NNDs
nndist(pp, by=marks(pp))
#Error in split.ppp(X %mark% idX, f = by, un = FALSE) :
# f must be a factor, a logical vector, a tessellation, a window, an image, or the name of a column of marks
錯誤消息給了你一個提示:
> nndist(pp, by=marks(pp))
Error in split.ppp(X %mark% idX, f = by, un = FALSE) :
f must be a factor, a logical vector, a tessellation, a window, an image, or the name of a column of marks
它表明marks
應該是一個因子、邏輯向量或鑲嵌。
經過檢查,結果證明pp$marks
是一個字符:
> str(pp$marks)
chr [1:6] "1994" "1994" "2005" "2005" "2005" "2005"
將其轉換為因子可以解決問題:
pp$marks <- as.factor(pp$marks)
nndist(pp, by=marks(pp))
1994 2005
[1,] 12021.108 1851.427
[2,] 12021.108 5321.291
[3,] 1851.427 10227.359
[4,] 5321.291 10227.359
[5,] 6732.880 11797.483
[6,] 11845.227 12344.920
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.