[R] Subsetting a matrix [Again!]

Achim Zeileis zeileis at ci.tuwien.ac.at
Tue Jul 15 12:53:14 CEST 2003


On Tuesday 15 July 2003 12:14, Ted Harding wrote:

> Hi Folks,
>
> People's suggestion of "drop=FALSE" seemed to do the trick
> (preserving "matrix" character when subestting to a row,
> i.e. creating 1xk matrix).
>
> However, I seem to have encountered a case where even this does
>
> not work:
> > mu<-c(1,2,3)
> > mu<-matrix(mu,nrow=1)
> > mu
>
>      [,1] [,2] [,3]
>      [1,]    1    2    3
>
> > iX1<-c(T,F,F); iX2<- !iX1
> >
> > mu1<-mu[iX1,drop=FALSE]; mu2<-mu[iX2,drop=FALSE];
> > mu1
>
> [1] 1
>
> > mu2
>
> [1] 2 3
>
> So now I still don't get my 1xk matrices, even though mu is a
> matrix and I've used "drop=FALSE". Why?

Because you are subsetting mu like a vector not like a matrix. The 
following produces the desired output:

R> mu1<-mu[,iX1,drop=FALSE]; mu2<-mu[,iX2,drop=FALSE];
R> mu1
     [,1]
[1,]    1
R> mu2
     [,1] [,2]
[1,]    2    3

Thus, iX1 and iX2 may only be used as the column index.
Z


> (I'm getting a bit bewildered by all this, and must get it pinned
> down: the code this will go into is too complicated to allow easy
> debugging if the subsetting does unpredicted things.)
>
> [BTW: Just in case anyone gets the thought that it might work if you
>  matrify iX1, iX2 e.g. iX1<-matrix(iX1,nrow=1) -- well, it doesn't!]
>
> Best wishes to all,
> Ted.
>
>
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
> Fax-to-email: +44 (0)870 167 1972
> Date: 15-Jul-03                                       Time: 11:14:05
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list