# [R] distance matrix and hclustering

David L Carlson dcarlson at tamu.edu
Mon Jul 30 18:46:09 CEST 2012

For basic hierarchical clustering use

groups <- hclust(dist(qmu))

But there are many options for function dist() and for function hclust() and
these will affect the results. For basic plotting use

plot(groups)

But as you will see, there are too many cases for the labels to be legible.
Hierarchical clustering gives you from 1 to ncases groups. You have to
decide where to cut the dendrogram to define a particular number of
clusters. Since you are clustering on a single value, you may find kmeans
clustering to be more useful You must specify the number of groups in
advance, but you can try several group sizes to see what makes the most

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of eliza botto
> Sent: Monday, July 30, 2012 1:44 AM
> To: r-help at r-project.org; smartpink111 at yahoo.com
> Subject: [R] distance matrix and hclustering
>
>
> Dear R Users,i am very new to R. I want your help on an issue regarding
> distance matrix and cluster analysis
> i had discharge data of 4 rivers(a,b,c,d) in 4 vectors each having 364
> values
> > dput(qmu)structure(list(a = c(0.26, 0.25, 0.25, 0.25, 0.24, 0.23,
> 0.22, 0.21, 0.21, 0.21, 0.2, 0.19, 0.19, 0.19, 0.19, 0.18, 0.18, 0.18,
> 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
> 0.17, 0.17, 0.17, 0.17, 0.18, 0.19, 0.19, 0.19, 0.2, 0.21, 0.21, 0.21,
> 0.21, 0.21, 0.21, 0.19, 0.19, 0.18, 0.17, 0.17, 0.15, 0.18, 0.2, 0.21,
> 0.2, 0.19, 0.19, 0.19, 0.2, 0.24, 0.3, 0.3, 0.3, 0.32, 0.34, 0.42,
> 0.46, 0.48, 0.67, 0.82, 0.79, 0.73, 0.69, 0.67, 0.67, 0.66, 0.64, 0.61,
> 0.58, 0.56, 0.55, 0.55, 0.55, 0.52, 0.49, 0.48, 0.51, 0.53, 0.52, 0.49,
> 0.48, 0.48, 0.46, 0.46, 0.44, 0.43, 0.43, 0.41, 0.48, 0.55, 0.57, 0.55,
> 0.56, 0.6, 0.64, 0.67, 0.73, 0.84, 0.94, 1.09, 1.24, 1.28, 1.19, 1.11,
> 1, 0.92, 0.86, 0.79, 0.76, 0.76, 0.76, 0.76, 0.92, 0.98, 1.03, 1.03,
> 1.03, 1.03, 1.07, 1.11, 1.24, 1.44, 2.12, 3.26, 15, 9.45, 5.07, 4.59,
> 3.5, 2.84, 2.54, 2.57, 3.01, 2.32, 2.32, 2.97, 2.92, 3.88, 4.76, 5.99,
> 3.74, 2.92, 2.65, 2.57, 2.97, 3.4, 4.13, 4.31, 3.89, 3.45, 3.01, 2.88,
> 2!
>  .5, 2.29, 2.39, 2.25, 2.02, 1.87, 1.87, 2.54, 2.69, 2.76, 3.18, 3.74,
> 4.59, 4.76, 4.36, 6.56, 5.07, 3.84, 3.55, 3.84, 3.84, 5.49, 5.32, 3.74,
> 3.31, 3.4, 3.26, 3.09, 2.69, 2.54, 2.46, 2.39, 2.25, 2.22, 2.22, 2.25,
> 2.29, 2.22, 2.18, 2.05, 2.18, 2.39, 2.18, 2.29, 2.11, 1.81, 1.6, 1.44,
> 1.41, 1.32, 1.37, 1.37, 1.65, 2.31, 2.25, 1.68, 1.41, 1.26, 1.15, 3.28,
> 1.93, 1.6, 1.53, 1.28, 1.13, 1.03, 1.03, 1.03, 1.03, 1, 0.96, 0.92,
> 0.87, 0.82, 0.79, 0.76, 0.73, 0.7, 0.67, 0.64, 0.64, 0.61, 0.61, 0.61,
> 1.76, 1.19, 1.24, 1.37, 1.68, 2.39, 2.05, 1.78, 1.58, 1.41, 1.39, 1.5,
> 1.41, 1.32, 1.19, 1.11, 1.02, 1.07, 4.57, 1.96, 1.68, 1.5, 1.37, 1.24,
> 1.11, 1.03, 0.96, 0.94, 2.93, 2.88, 2.92, 2.76, 2.02, 1.71, 1.5, 1.37,
> 1.22, 1.09, 1, 0.94, 0.87, 0.81, 0.76, 0.73, 0.7, 0.67, 0.61, 0.58,
> 0.57, 0.55, 0.53, 0.51, 0.48, 0.47, 0.44, 0.43, 0.43, 0.41, 0.41, 0.38,
> 0.4, 0.4, 0.42, 0.42, 0.41, 0.46, 0.53, 0.55, 0.52, 0.49, 0.51, 0.53,
> 0.55, 0.7, 1.03, 1.03, 1.17, 1.24, 1.19, 1.11, 1.03, 0.98, 0.92, 0.84,!
>   0.79, 0.75, 0.7, 0.67, 0.61, 0.58, 0.56, 0.56, 0.55, 0.53, 0.51, 0.48
> , 0.46, 0.43, 0.41, 0.38, 0.37, 0.35, 0.34, 0.32, 0.31, 0.3, 0.29,
> 0.28, 0.27, 0.25, 0.26, 0.24, 0.23, 0.22, 0.22, 0.21, 0.21, 0.21), b =
> c(0.19, 0.19, 0.19, 0.18, 0.18, 0.18, 0.17, 0.17, 0.17, 0.17, 0.16,
> 0.17, 0.17, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,
> 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.14, 0.14, 0.14, 0.14, 0.14,
> 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.15, 0.15,
> 0.15, 0.15, 0.15, 0.15, 0.15, 0.16, 0.16, 0.17, 0.17, 0.18, 0.18, 0.18,
> 0.18, 0.18, 0.18, 0.18, 0.18, 0.19, 0.21, 0.21, 0.21, 0.22, 0.23, 0.24,
> 0.24, 0.23, 0.24, 0.24, 0.25, 0.25, 0.25, 0.28, 0.29, 0.29, 0.3, 0.31,
> 0.31, 0.34, 0.41, 0.46, 0.51, 0.57, 0.61, 0.64, 0.67, 0.7, 0.76, 0.82,
> 0.86, 1.05, 1.24, 1.05, 0.94, 0.92, 0.9, 0.86, 0.82, 0.76, 0.76, 0.76,
> 0.78, 0.82, 0.9, 1.07, 1.76, 3.13, 3.64, 3.45, 3.01, 2.39, 2.02, 1.87,
> 2.11, 2.02, 1.78, 1.63, 1.53, 1.63, 4.84, 12.5, 8.11, 3.89, 2.73, 2.11,
> 1.96, 3.17, 2.65, 2.54, 3.01, 3.31, 3.6, 3.36, 2.76, 2.39, 2.11, 2.!
>  25, 2.08, 1.99, 2.11, 2.36, 3.13, 7.16, 5.39, 5.52, 5.32, 4.25, 3.45,
> 3.26, 3.18, 3.74, 4.35, 5.79, 5.45, 4.42, 3.84, 3.36, 2.84, 2.39, 3.84,
> 3.18, 3.22, 2.97, 2.73, 2.65, 2.92, 4.33, 3.01, 3.01, 3.26, 3.09, 3.6,
> 3.64, 4.05, 4.25, 4.48, 3.69, 3.74, 3.6, 3.18, 2.76, 4.11, 2.92, 2.69,
> 2.73, 2.69, 3.93, 2.69, 2.18, 2.52, 2.69, 1.99, 2.57, 1.81, 1.55, 1.44,
> 1.37, 1.28, 1.19, 1.19, 1.03, 1.03, 1, 0.94, 0.89, 0.87, 0.86, 0.86,
> 2.3, 1.55, 1.19, 1.11, 1.5, 1.39, 1.22, 1.24, 1.07, 1.02, 0.96, 0.92,
> 1.34, 1.15, 1.03, 2.06, 1.76, 1.3, 1.15, 1.05, 0.98, 0.92, 0.89, 0.84,
> 0.81, 0.76, 0.73, 1.59, 5.2, 3.01, 2.05, 1.65, 1.68, 5.29, 2.73, 1.96,
> 1.6, 1.41, 1.28, 1.15, 1.11, 1.13, 1.09, 1.03, 6.99, 10.6, 5.39, 3.45,
> 2.5, 1.87, 1.68, 1.78, 1.53, 1.41, 1.3, 1.17, 1.05, 0.98, 0.92, 0.9,
> 0.87, 0.86, 0.82, 0.78, 0.75, 0.72, 0.67, 0.82, 1.6, 0.89, 0.94, 0.96,
> 0.92, 0.87, 0.82, 0.79, 0.75, 0.7, 0.67, 0.64, 0.61, 0.58, 0.56, 0.53,
> 0.51, 0.48, 0.47, 0.46, 0.43, 0.41, 0.41, 0.68, 16.3, 17.2, 6.05, 3.6!
>  9, 2.92, 2.25, 1.87, 1.63, 1.46, 1.32, 1.19, 1.07, 1, 0.94, 0.89, 0.87
> , 0.86, 0.81, 0.76, 0.73, 0.7, 0.7, 0.7, 0.7, 0.7, 0.67, 0.67, 0.66,
> 0.64, 0.61, 0.58, 0.56, 0.55, 0.53, 0.51, 0.48, 0.46, 0.44, 0.43, 0.43,
> 0.41, 0.4, 0.38, 0.37, 0.36, 0.35, 0.34, 0.33, 0.32, 0.31, 0.31, 0.3,
> 0.3, 0.29, 0.29, 0.28, 0.27, 0.27), c = c(0.27, 0.25, 0.25, 0.25, 0.24,
> 0.24, 0.23, 0.22, 0.22, 0.21, 0.21, 0.21, 0.21, 0.21, 0.2, 0.19, 0.19,
> 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19,
> 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.17, 0.17,
> 0.17, 0.16, 0.15, 0.15, 0.15, 0.14, 0.14, 0.14, 0.14, 0.13, 0.13, 0.13,
> 0.13, 0.13, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12,
> 0.12, 0.13, 0.14, 0.15, 0.15, 0.16, 0.17, 0.18, 0.2, 0.21, 0.23, 0.25,
> 0.27, 0.29, 0.31, 0.32, 0.31, 0.32, 0.32, 0.32, 0.32, 0.32, 0.32, 0.32,
> 0.32, 0.32, 0.32, 0.32, 0.3, 0.28, 0.28, 0.3, 0.33, 0.35, 0.4, 0.43,
> 0.47, 0.52, 0.52, 0.51, 0.53, 0.56, 0.58, 0.58, 0.55, 0.51, 0.47, 0.47,
> 0.47, 0.46, 0.48, 0.51, 0.56, 0.61, 0.73, 1, 1.34, 1.73, 8.71, !
>  9.98, 11.1, 5.92, 3.8, 2.84, 2.69, 3.35, 3.89, 4.8, 5.79, 5.2, 4.36,
> 3.69, 3.5, 2.92, 2.5, 2.32, 2.65, 2.92, 2.8, 3.94, 5.39, 3.55, 2.92,
> 4.65, 6.15, 4.03, 2.88, 2.39, 2.08, 1.93, 1.81, 1.55, 1.34, 1.44, 2.29,
> 2.92, 2.92, 2.73, 2.57, 2.73, 3.01, 3.18, 3.09, 6.32, 10.9, 4.25, 3.22,
> 2.92, 7.77, 20.8, 6.27, 3.84, 3.01, 2.69, 2.39, 2.32, 2.32, 2.32, 2.15,
> 2.32, 2.32, 2.57, 3.15, 2.61, 2.05, 1.76, 1.53, 1.65, 1.71, 1.73, 1.63,
> 1.76, 1.87, 1.9, 1.84, 1.81, 1.84, 1.73, 2.08, 1.65, 1.58, 1.5, 1.41,
> 1.37, 1.28, 1.32, 1.19, 1.05, 1.09, 1.09, 1, 0.98, 0.94, 0.92, 0.9,
> 0.87, 0.86, 0.82, 0.79, 0.79, 0.79, 1.09, 1.05, 1.05, 1.03, 0.96, 0.9,
> 0.82, 0.79, 0.76, 0.76, 0.73, 0.72, 0.67, 0.63, 0.58, 0.57, 0.55, 0.52,
> 0.51, 0.51, 0.49, 0.48, 0.49, 0.49, 0.51, 0.51, 0.51, 0.51, 0.51, 0.51,
> 1.33, 0.92, 0.76, 0.76, 1.16, 1.39, 1.34, 1.26, 1.17, 1.07, 1, 0.92,
> 0.86, 0.81, 0.76, 0.72, 0.67, 0.64, 0.61, 0.58, 0.56, 0.53, 0.51, 0.49,
> 0.48, 0.46, 0.43, 0.43, 0.43, 0.42, 0.41, 0.38, 0.36, 0.34, 0.32, 0.!
>  32, 0.32, 0.31, 0.31, 0.29, 0.28, 0.28, 0.27, 0.27, 0.27, 0.26, 0.26,
> 0.25, 0.26, 0.25, 0.25, 0.24, 0.23, 0.22, 0.21, 0.19, 0.19, 0.19, 0.18,
> 0.18, 0.21, 0.4, 0.41, 0.43, 0.43, 0.43, 0.44, 0.46, 0.49, 0.51, 0.51,
> 0.55, 0.56, 0.53, 0.51, 0.48, 0.46, 0.42, 0.41, 0.41, 0.38, 0.35, 0.36,
> 0.34, 0.34, 0.32, 0.31, 0.29, 0.27, 0.26, 0.24, 0.24, 0.21, 0.19, 0.19,
> 0.18, 0.18, 0.18, 0.17, 0.16, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.14,
> 0.14),     d = c(0.13, 0.13, 0.13, 0.13, 0.12, 0.12, 0.13, 0.13, 0.12,
> 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12,     0.12,
> 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12,     0.12, 0.13,
> 0.13, 0.13, 0.16, 0.2, 0.19, 0.17, 0.17, 0.16,     0.16, 0.16, 0.16,
> 0.18, 0.17, 0.16, 0.16, 0.16, 0.16, 0.15,     0.15, 0.14, 0.13, 0.13,
> 0.16, 0.15, 0.15, 0.14, 0.14, 0.14,     0.14, 0.14, 0.14, 0.14, 0.14,
> 0.14, 0.14, 0.13, 0.13, 0.13,     0.12, 0.12, 0.12, 0.12, 0.11, 0.12,
> 0.13, 0.14, 0.16, 0.16,     0.17, 0.23, 0.28, 0.28, 0.31, 0.32, 0.37,
> 0.41, 0.37, 0.36,     0.34, 0.39, 0.4, 0.37, 0.34, 0.33, 0.32, 0.2!
>  9, 0.28, 0.28,     0.28, 0.28, 0.28, 0.26, 0.27, 0.28, 0.28, 0.36,
> 0.52, 0.63,     1.12, 1.02, 1.19, 1.51, 1.72, 1.97, 3.11, 2.77, 2.19,
> 1.86,     1.62, 1.39, 1.16, 1.04, 0.96, 0.91, 0.9, 0.93, 1.21, 1.31,
> 1.46, 1.59, 1.46, 1.49, 1.54, 1.62, 1.83, 2.14, 2.22, 2.16,     2.14,
> 2.33, 2.47, 2.14, 1.97, 2, 2.53, 2.81, 2.85, 2.81,     2.92, 3, 3.21,
> 3.43, 3.3, 3.17, 2.4, 2.02, 2.66, 3.56, 8.31,     5.92, 5.99, 5.92,
> 4.93, 4.98, 5.19, 4.41, 5.57, 4.87, 4.04,     3.79, 3.61, 3.89, 3.7,
> 3.66, 3.7, 3.89, 4.47, 7.77, 5.44,     3.89, 3.17, 2.92, 2.62, 2.26,
> 2.14, 2.16, 2.14, 2.16, 2.11,     1.97, 1.89, 1.83, 1.86, 1.97, 1.83,
> 1.57, 1.29, 1.14, 1.04,     0.98, 1, 0.98, 0.96, 1, 1.39, 1.21, 1.1,
> 1.04, 0.98, 0.94,     0.93, 0.93, 0.93, 0.9, 0.9, 0.9, 0.88, 0.86,
> 0.83, 0.83,     0.83, 0.8, 0.78, 0.74, 0.69, 0.64, 0.61, 0.6, 0.61,
> 0.6,     1.99, 2.3, 1.54, 1.54, 1.44, 1.29, 1.14, 1.04, 0.96, 0.96,
> 1.08, 1.1, 1.08, 1.02, 0.94, 0.91, 0.86, 0.8, 0.77, 0.7,     0.64,
> 0.61, 3.28, 1.62, 1!
>  .64, 1.49, 1.39, 1.29, 1.14, 1.04,     0.96, 0.9, 0.85, 0.78, 0.74, 0.
> 7, 0.67, 0.63, 0.58, 0.55,     0.52, 0.51, 0.49, 0.48, 0.45, 0.44,
> 0.43, 0.41, 0.4, 1.29,     11.6, 14, 4.68, 2.92, 2.19, 1.78, 1.54,
> 1.41, 1.29, 1.14,     1.04, 0.98, 0.94, 0.9, 0.83, 0.78, 0.74, 0.67,
> 0.64, 0.61,     0.58, 0.55, 0.51, 0.46, 0.46, 0.45, 0.44, 0.44, 0.43,
> 0.41,     0.4, 0.39, 0.39, 0.39, 0.39, 0.36, 0.36, 0.36, 0.36, 0.36,
> 0.35, 0.35, 0.35, 0.34, 0.36, 0.35, 0.34, 0.34, 0.34, 0.34,     0.34,
> 0.34, 0.35, 0.35, 0.36, 0.36, 0.37, 0.36, 0.36, 0.35,     0.34, 0.34,
> 0.32, 0.32, 0.32, 0.31, 0.31, 0.31, 0.31, 0.31,     0.31, 0.29, 0.28,
> 0.26, 0.25, 0.24, 0.22, 0.2, 0.19, 0.18,     0.17, 0.16, 0.15)), .Names
> = c("a", "b", "c", "d"), class = "data.frame", row.names = c(NA, -
> 364L))
>
> by using "as.matrix", i converted by data into a matrix. Now, I want to
> create a distance matrix. My data as shown above contains 364 rows and
> 4 columns. I have to create distance matrix based on the values of 2nd
> column.
> secondly, based on this distance matrix i would like to do
> "hclustering", to see which of the rivers have similar discharge
> patterns. more precisely, i need to see river(a,b,c,d) splitted in
> clusters with river having similar discharge patterns in one cluster.