Skip to contents

This function uses distm to create a distance matrix and hclust with cutree to assign each point to a cluster.

Usage

dwc_point_cluster(
  df,
  longitude,
  latitude,
  distm_method = geosphere::distHaversine,
  hclust_method = "average",
  k = NULL,
  h = NULL
)

Arguments

df

A data frame object.

longitude

The name of the column in df containing the longitude values.

latitude

The name of the column in df containing the latitude values.

distm_method

The method passed to distm for calculating the distance between points.

hclust_method

The method passed to hclust for performing the hierarchical clustering.

k

The number of desired groups passed to cutree.

h

The distance cutoff passed to cutree.

Value

A data frame with a new column cluster that shows the cluster each point has been assigned to.

Examples

df <-
  data.frame(
    longitude = c(116.018, 116.010, 116.011, 115.969, 115.976, 115.973),
    latitude = c(-31.709, -31.707, -31.713, -32.339, -32.339, -32.344)
  )

dwc_point_cluster(
  df = df,
  longitude = "longitude",
  latitude = "latitude",
  hclust_method = "average",
  h = 5000
)
#> [1] "Making distance matrix - this can be slow"
#> [1] "Performing hierarchical clustering"
#>   longitude latitude cluster
#> 1   116.018  -31.709       1
#> 2   116.010  -31.707       1
#> 3   116.011  -31.713       1
#> 4   115.969  -32.339       2
#> 5   115.976  -32.339       2
#> 6   115.973  -32.344       2