我有一個數據幀(DF),其具有三個柱喜歡這樣:(所有數字隨機)獲取的緯度和經度的質心在數據幀中
ID Lat Lon
1 25.32 -63.32
1 25.29 -64.21
1 24.12 -62.43
2 12.42 54.64
2 12.11 53.43
. .... ....
基本上我想有像每ID的質心所以:
ID Lat Lon Cent_lat Cent_lon
1 25.32 -63.32 25.31 -63.25
1 25.29 -64.21 25.31 -63.25
1 24.12 -62.43 25.31 -63.25
2 12.42 54.64 12.20 53.60
2 12.11 53.43 12.20 53.60
我厭倦了以下內容:
library(geosphere)
library(rgeos)
library(dplyr)
df1 <- by(df,df$ID,centroid(df$Lat, df$Long))
但是,這給了我這個錯誤:
Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘centroid’ for signature ‘"numeric"’
我甚至累
df1 <- by(df,df$ID,centroid(as.numeric(df$Lat), as.numeric(df$Long)))
但是,這給了我這個錯誤:
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘centroid’ for signature ‘"function"’
是不是三個點的平均值(平均值(長),平均值(lat))的質心? – lmo
對於大多數情況,我們有三個以上的點數,如果地球是平坦的,平均方法就可以工作:-) –
要使用'centroid',需要一個poligon作爲矩陣對象,或者每個點需要一個適當的rownames數據框 – Robert