【发布时间】:2016-12-06 14:06:29
【问题描述】:
我有一个数据框 (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))
但这给了我这个错误:
(函数(类、fdef、mtable)中的错误: 无法为签名“numeric”找到函数“centroid”的继承方法
我什至累了
df1 <- by(df,df$ID,centroid(as.numeric(df$Lat), as.numeric(df$Long)))
但这给了我这个错误:
(函数(类,fdef,mtable)中的错误: 无法为签名“函数”找到函数“质心”的继承方法
【问题讨论】:
-
三点的质心不就是它们分量的平均值吗(mean(long), mean(lat))?
-
在大多数情况下,我们有超过三个点,如果地球是平的,平均方法会起作用:-)
-
要使用
centroid,您需要一个 poligon 作为矩阵对象,或者一个为每个点提供适当行名的数据框