[R] subscripting a one column matrix drops dimension

Pedroso MOACIR moacir at supagro.inra.fr
Tue Oct 21 15:24:34 CEST 2008


Hi all,

Why subscripting a one column matrix drops one dimension?

 > x<- matrix(rnorm(100), ncol=1)

 > str(x)
 num [1:100, 1] -0.413 -0.845 -1.625 -1.393  0.507 ...

 > str(x[20:30,])
 num [1:11] -0.315 -0.693 -0.771  0.448  0.204 ...

 > str(x[20:30])
 num [1:11] -0.315 -0.693 -0.771  0.448  0.204 ...


This breaks:

 > cov(x)
          [,1]
[1,] 0.9600812

 > cov(x[20:30])
Erreur dans cov(x[20:30]) : fournir 'x' et 'y' ou bien 'x' en matrice
 

And this behavior is braking function clustIndex (when used with 
unidimensional data),
 from the package cclust, file Rindexes.R, lines 137-147:

    ttww <- function(x, clsize, cluster)
      {
        n <- sum(clsize)
        k <- length(clsize)
        w<-0
        tt <- cov(x)*n
        for (l in 1:k)
          w<- w+cov(x[cluster==l,])*clsize[l]
        zttw <- list(tt=tt, w=w)
        return(zttw)
      }

Any way around it?

Thank you.

Moacir Pedroso Jr.
Embrapa - Empresa Brasileira de Pesquisa Agropecuária
(on leave at INRA - Instutute National de la Recherche Agronomique)



More information about the R-help mailing list