[R] Species names in distance matrix

Peter Ehlers ehlers at ucalgary.ca
Sat Jul 14 01:31:05 CEST 2012

On 2012-07-13 15:55, Katharine Miller wrote:
> I have a function that requires a distance matrix of class dist with
> species names as row names.  For the life of me, I cannot figure out how to
> get dist() to include species names.
> I am sure this must be easy, because a lot of packages and functions out
> there require dist objects to have row names.  Any help would be MUCH
> appreciated.
> - Katharine

You should be able to just set the 'Labels' attribute:

   x <- matrix(rnorm(100), nrow=5)
   d <- dist(x)
   attr(d, "Labels") <- LETTERS[1:5]

Alternatively, convert d to a matrix, assign rownames
and convert back to a dist:

   m <- as.matrix(d)
   rownames(m) <- letters[1:5]
   d1 <- as.dist(m)

Peter Ehlers

More information about the R-help mailing list