I have a case where the number of rows of the distance matrix and the number of segments is different:
$ wc -l forms_ni_seg20_matrix_seg.csv
4139 forms_ni_seg20_matrix_seg.csv
vs.
$ ogrinfo -so -al forms_ni_seg20.shp
INFO: Open of `forms_ni_seg20.shp'
using driver `ESRI Shapefile' successful.
Layer name: forms_ni_seg20
Metadata:
DBF_DATE_LAST_UPDATE=2018-02-28
Geometry: Polygon
Feature Count: 4821
Extent: (1567218.101930, 5390724.144650) - (2089218.101930, 6194224.144650)
Layer SRS WKT:
PROJCS["Transverse_Mercator",
GEOGCS["GCS_grs80",
DATUM["New_Zealand_Geodetic_Datum_2000",
SPHEROID["GRS_1980",6378137,298.257222101]],
PRIMEM["Greenwich",0],
UNIT["Degree",0.017453292519943295]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",173],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",1600000],
PARAMETER["false_northing",10000000],
UNIT["Meter",1]]
segment_id: Integer64 (11.0)
> segm$class <- hclust_cut
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 1L, 2L, 3L, 3L, 3L, :
replacement has 4138 rows, data has 4821