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)
moacir at supagro.inra.fr
2008-Oct-21 19:32 UTC
[R] subscripting a one column matrix drops dimension
Hi all,
Well, I just sent a help msg to the list with this subject,
but I think I figured out a way to solve my problem
(although I still have no clue if the behavior I described
is correct).
What I did was to replace occurrences of (for example)
cov(x[sub, ])
with
cov(matrix(x[sub, ], ncol=ncol(x))
It works.
I updated my source version of the cclust package. It
may be of interest to somebody else, but I don't know
how to submit to the package mantainer.
Thank you.
Moacir Pedroso Jr.
Embrapa - Empresa Brasileira de Pesquisa Agropecu?ria
(on leave at INRA - Instutute National de la Recherche Agronomique)
Charles C. Berry
2008-Oct-21 19:37 UTC
[R] subscripting a one column matrix drops dimension
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
On 22/10/2008, at 2:24 AM, 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 ...Try x[20:30,1,drop=FALSE] cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
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?