-2

I have a lot of points on map (long/Lat), and i want to cluster them to groups, something like k-means.

There is any way to insert points (long/Lat) and number of groups, and get for each point to which group she is belongs?

zx8754
  • 46,390
  • 10
  • 104
  • 180
AsSAASA
  • 25
  • 7
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Jul 07 '16 at 07:45
  • This might help. http://stackoverflow.com/questions/28672399/spatial-clustering-in-r-simple-example – Ram Chandra Jul 08 '16 at 04:18

2 Answers2

0
library(ggplot2)

# Construct Data
set.seed(23)
lat <- c(seq(from = 16.3478, to = 14.1329876, length.out = 500),
seq(from = 18.5478, to = 19.567, length.out = 500))
lat <- sample(x = lat, size = 100)

lon <- seq(from = 45.987, to = 46.98237, length.out = 1000)
lon <- sample(x = lon, size = 100)

# Place inside data.frame
df_latlon <- data.frame(lat, lon)

cluster_latlon <- kmeans(x = df_latlon, centers = 2, nstart = 20)
df_latlon <- cbind(df_latlon, cluster_latlon$cluster)

# Output ggplot with colored values
ggplot(df_latlon) + 
geom_point(aes(lat, lon, color = as.factor(cluster_latlon$cluster))) 

cluster_latlon$centers
cluster_latlon$cluster
coatless
  • 18,823
  • 13
  • 63
  • 79
AsSAASA
  • 25
  • 7
0

Your question is essentially "how do I do clustering".

It cannot be answered concisely, but you should read a textbook on clustering.

k-means is not appropriate for latitude and longitude, because it is a least-squares approach, but 1 degree latitude is not equal to 1 degree of longitude (usually). Use PAM, hierarchical clustering, DBSCAN, etc.

Has QUIT--Anony-Mousse
  • 73,503
  • 12
  • 131
  • 189