[R] How to extract diagonals

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jun 20 14:11:39 CEST 2007


On Wed, 2007-06-20 at 13:26 +0200, Birgit Lemcke wrote:
> Hello,
> 
> I am using Mac OS X on a power book and R 2.5.0
> 
> I try to extract a diagonal from a dissimilarity matrix made with  
> dsvdis, with this code:
> 
> diag(DiTestRR)
> 
> But I get this error message:
> 
> Fehler in array(0, c(n, p)) : 'dim' spezifiziert ein zu groes Array
> 
> english:
> 
> Error in array(0, c(n, p)) : 'dim' specifies a too big array.
> 
> Is there a limit to extract diagonals?

The returned object is not a matrix, but an object of class "dist" which
doesn't store the diagonals or the upper triangle of the dissimilarity
matrix to save memory. You need to convert the dist object to a matrix
first, then extract the diagonal. But, as this shows:

> require(labdsv)
> ?dsvdis
> data(bryceveg)
> ?dsvdis
> dis.bc <- dsvdis(bryceveg,index="bray/curtis")
Warning in symbol.For("dsvdis") : 'symbol.For' is not needed: please
remove it
> diag(as.matrix(dis.bc))

This is meaningless as the diagonals are all zero, as they should be;
this is the distance between a site and itself.

> 
> I hope somebody will help me!

So perhaps you could explain why you want the diagonal. It would be
easier to just do:

diags <- rep(0, length = nrow(bryceveg))

That will be without the sample labels, but that is easily rectified

> names(diags) <- rownames(bryceveg)
> all.equal(diags, diag(as.matrix(dis.bc)))
[1] TRUE

So you'll have to reformulate your question if this is not what you
wanted.

A word of warning, do not do diag(dis.bc)) on the above as it brought my
Linux box to it's knees trying to do something silly - easily
recoverable, but beware.

HTH

G

> 
> Greetings
> 
> Birgit Lemcke

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list