[R] subscripting a one column matrix drops dimension

Charles C. Berry cberry at tajo.ucsd.edu
Tue Oct 21 21:37:52 CEST 2008



Two solutions:

 	1) Follow the Posting Guide

 	2) Use the function help.request() [new to R-2.8.0] to figure how
            what steps to take.


Each method leads to the solution here:


 	http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f


HTH,

Chuck

On Tue, 21 Oct 2008, Pedroso MOACIR wrote:

> 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)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list