我需要幫助,非常感謝任何指導!
我的目標是創建一個函式,利用現有函式 poly2nb() 計算多邊形 shapefile 的相鄰鄰居的空間移動平均值。該函式必須靈活,以便它可以呼叫作業 shapefile 中的任何屬性。我不知道如何解決我收到的錯誤: .subset2(x, i, exact = exact) 中的錯誤 : no such index at level 3,當我嘗試呼叫我的函式時。我完全不知道如何解決這個問題。
我正在使用芝加哥社區資料 mulypolygon 的 shapefile,其中包含以下屬性:
?Community Area ID
?Community Name
?Tract Cnt
?Pop2014
?Hisp14P
?PerCInc14
?Assault
?DiabetM
?FirearmM
?LungCancer
我的功能A
a_spmvavg <- function(shapefile, my_attribute) {
nbs = poly2nb(shapefile)
a_matrix = nb2mat(nbs, style='W', zero.policy=TRUE)
a_val = shapefile[[my_attribute]]
a_average1 = a_matrix%*%a_val
a_newdf = cbind(shapefile, a_average1)
return (a_newdf)
}
嘗試呼叫函式:
a_spmvavg(chicagoshp,chicagoshp$Assault)
錯誤輸出
.subset2(x, i, exact = exact) 中的錯誤:第 3 級沒有這樣的索引
4.(function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, i, exact = exact))(x, ... , 準確 = 準確)
3. [[.data.frame(shapefile, my_attribute)
2.shapefile[[my_attribute]]
1.a_spmvavg(chicagoshp, chicagoshp$Assault)
uj5u.com熱心網友回復:
你這樣稱呼它:
a_spmvavg(chicagoshp,chicagoshp$Assault)
你的功能是這樣定義的:
a_spmvavg <- function(shapefile, my_attribute) {
所以當你到達這里時:
a_val = shapefile[[my_attribute]]
...你在做chicagoshp[[chicagoshp$Assault]]。那是你要的嗎?
或者您是否應該傳遞列的名稱,例如:a_spmvavg(chicagoshp,"Assault")在這種情況下,shapefile[[myAttribute]]評估chicagoshp[["Assault"]]將列值作為向量獲取到哪個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/531340.html
下一篇:無法使用tmap繪制人口普查區
