I have a dataframe (df) which has three column likes so: (all numbers random)
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
. .... ....
Basically I wanted to have the centroid per ID like so:
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
I tired the following:
library(geosphere)
library(rgeos)
library(dplyr)
df1 <- by(df,df$ID,centroid(df$Lat, df$Long))
But this gave me this error:
Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘centroid’ for signature ‘"numeric"’
I even tired
df1 <- by(df,df$ID,centroid(as.numeric(df$Lat), as.numeric(df$Long)))
But this gave me this error:
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘centroid’ for signature ‘"function"’
centroid
you need a poligon as matrix object, or a dataframe with appropriate rownames for each point – Robert