我有一个数据帧(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