title: Clustering using lat/lon data in R
body: <p>I am very new to R.Currently I am doing cluster analysis using latitude and longitude data then plot the value in google map. But my data point is very much limited...only 20 points.
  As per my knowledge I want to do it in using k-means algo and for distance calculation purpose I want to use Haversian distance (<a href="https://www.slideshare.net/AnbarasanS2/clusteranalysis-58192369).I" rel="nofollow noreferrer">https://www.slideshare.net/AnbarasanS2/clusteranalysis-58192369).I</a> also trying Density Based Clustering but gives me very poor result.So,I want to stay with k-means.My dataset and code is given below - </p>
  
  <pre><code>1   27.9745 79.0028
  2   29.4716 77.7642
  3   30.9688 76.5256
  4   29.4716 77.7642
  5   29.4716 77.7642
  6   29.4716 77.7642
  7   29.4716 77.7642
  8   25.5648 83.4477
  9   26.2946 79.041
  10  22.5293 77.178
  11  26.2946 79.041
  12  30.7896 76.4973
  13  26.2946 79.041
  14  28.1856 72.2447
  15  28.1856 72.2447
  16  28.1856 72.2447
  17  28.1856 72.2447
  18  28.1856 72.2447
  19  28.1856 72.2447
  20  28.1856 72.2447
  </code></pre>
  
  <p>Code is -</p>
  
  <pre><code>geodata = read.csv('test.csv')
  
  #K-means clustering
  #Compute the distance matrix using Geosphere package.
  geo.dist &lt;- function(df) {
    require(geosphere)
    d &lt;- function(i,z) {
      dist &lt;-rep(0,nrow(z))
      dist[i:nrow(z)] &lt;-
        distHaversine(z[i:nrow(z),1:2],z[i,1:2])
      return(dist)
    }
    dm &lt;- do.call(cbind,lapply(1:nrow(df), d, df))
    return(as.dist(df))
  }
  
  distance.matrix &lt;-geo.dist(geodata[,c(2,3)])
  
  #Determine the no.of clusters
  wssplot.distancematrix &lt;- function(data, nc = 15, seed = 1234) {
    wss &lt;-rep(0,15)
    for (i in 2:nc) {
      set.seed(seed)
      wss[i] &lt;- sum(kmeans(data, centers = i)$withinss)
    }
    plot(1:nc,wss,
         type = "b")
  }
  
  wssplot.distancematrix(distance.matrix)
  </code></pre>
  
  <p>But got this error - </p>
  
  <blockquote>
    <p>Error in dimnames(df) &lt;- if (is.null(labels)) list(seq_len(size),
    seq_len(size)) else list(labels,  :    length of 'dimnames' [1] not
    equal to array extent In addition: Warning message: In df[row(df) >
    col(df)] &lt;- x :</p>
  </blockquote>
  
  <p>Show Traceback</p>
  
  <p>Rerun with Debug
   Error in dimnames(df) &lt;- if (is.null(labels)) list(seq_len(size), seq_len(size)) else list(labels,  : 
    length of 'dimnames' [1] not equal to array extent </p>
  
  <p>How to create k-means clustering and plot the values in google map.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Regards,
  Nikita</p>