[R] dissimilarity matrices
Gavin Simpson
gavin.simpson at ucl.ac.uk
Fri Nov 10 09:56:13 CET 2006
On Thu, 2006-11-09 at 17:50 +0000, Kris Lockyear wrote:
> Dear All,
>
> I have a dissimilarity matrix which I happily convert to a distance object
> by running:
>
> X <- as.dist(Y)
>
> and I can happily now run either hclust(X) or agnes(X).
>
> So that the various bits of output are labelled correctly I would dearly
> like to be able to give names to the columns and rows of X, as would happen
> if I ran:
>
> mydata<-read.table("clipboard",header=T)
> mydata2<-t(mydata)
> mydists<-dist(mydata2)
I assume Y doesn't have column/row names then? So give Y some col/row
names before you convert it to a dist object:
> mat <- matrix(seq(0.1, 0.9, by = 0.1), ncol = 3)
> diag(mat) <- 0
> mat
[,1] [,2] [,3]
[1,] 0.0 0.4 0.7
[2,] 0.2 0.0 0.8
[3,] 0.3 0.6 0.0
> distmat <- as.dist(mat)
> distmat
1 2
2 0.2
3 0.3 0.6
> rownames(mat) <- colnames(mat) <- LETTERS[1:3]
> distmat <- as.dist(mat)
> distmat
A B
B 0.2
C 0.3 0.6
or you can modify the "Labels" attribute of your object X, *after* you
have done as.dist(Y), e.g. following on from the above example:
> attr(distmat, "Labels") <- paste("Samp", 1:3, sep = "")
> distmat
Samp1 Samp2
Samp2 0.2
Samp3 0.3 0.6
HTH
G
>
> Many thanks for your help.
>
> Best wishes, Kris Lockyear.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [t] +44 (0)20 7679 0522
ECRC [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list