[R] subscripting a one column matrix drops dimension
jim holtman
jholtman at gmail.com
Tue Oct 21 21:46:42 CEST 2008
You need drop=FALSE: see ?'['
> x<- matrix(rnorm(100), ncol=1)
> str(x)
num [1:100, 1] -0.626 0.184 -0.836 1.595 0.330 ...
> str(x[20:30,])
num [1:11] 0.5939 0.9190 0.7821 0.0746 -1.9894 ...
> str(x[20:30,,drop=FALSE])
num [1:11, 1] 0.5939 0.9190 0.7821 0.0746 -1.9894 ...
>
On Tue, Oct 21, 2008 at 9:24 AM, Pedroso MOACIR <moacir at supagro.inra.fr> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list