I'd welcome some comments or advice regarding the situation described below. The following illustrates what seems to me to be an inconsistency in the behaviour of matrix subsetting: > Z<-matrix(c(1.1,2.1,3.1,1.2,2.2,3.2,1.3,2.3,3.3),nrow=3) > Z [,1] [,2] [,3] [1,] 1.1 1.2 1.3 [2,] 2.1 2.2 2.3 [3,] 3.1 3.2 3.3 > dim(Z) [1] 3 3 > Z0<-Z[c(T,F,F),c(F,T,T)] > Z0 [1] 1.2 1.3 > dim(Z0) NULL whereas, of course, with > Z1<-Z[c(T,T,F),c(F,T,T)] > Z1 [,1] [,2] [1,] 1.2 1.3 [2,] 2.2 2.3 > dim(Z1) [1] 2 2 i.e. a fully-paid-up matrix. What I would have expected is that Z0 should come out as a 1x2 matrix: [,1] [,2] [1,] 1.2 1.3 with dim(Z0) --> [1] 1 2 but it does not -- it does not have matrix status. I know it will try to behave politely if forced into matrix-algebra society, but it betrays its lack of true poise in solecisms like the following: > Z0%*%Z1[1:2,1] [,1] [1,] 4.3 > Z1[1:2,1]%*%Z0 [,1] [1,] 4.3 One of these should be a scalar (in fact the first, given how Z0 was created), and the other a 2x2 matrix. Z0 simply does not know that you have to behave differently if seated on someone's right rather than on their left. Now of course one can send Z0 away for remedial training ( t() ): > Z0<-t(Z0) > Z0 [,1] [,2] [1,] 1.2 1.3 > Z0%*%Z1[1:2,1] [,1] [1,] 4.3 > Z1[1:2,1]%*%Z0 [,1] [,2] [1,] 1.44 1.56 [2,] 2.64 2.86 and now it does know how to behave. But it would save some bother (and having to worry about this sort of thing in the midst of complex matrix algebra) if matrix subsetting worked in a consistent way for all possible subsets including cases which should result in 1xk or kx1 matrices (where, by the way, we could have k=1 and get a 1x1 matrix). Is there perhaps some global option which regulates this sort of behaviour? Or is the underclass always with us? With thanks, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 14-Jul-03 Time: 10:59:04 ------------------------------ XFMail ------------------------------
On Monday 14 July 2003 11:59, you wrote:> I'd welcome some comments or advice regarding the situation described > below. > > The following illustrates what seems to me to be an inconsistency > in the behaviour of matrix subsetting: > > ? > Z<-matrix(c(1.1,2.1,3.1,1.2,2.2,3.2,1.3,2.3,3.3),nrow=3) > ? > Z > ? ? ? ?[,1] [,2] [,3] > ? [1,] ?1.1 ?1.2 ?1.3 > ? [2,] ?2.1 ?2.2 ?2.3 > ? [3,] ?3.1 ?3.2 ?3.3 > ? > dim(Z) > ? [1] 3 3 > > ? > Z0<-Z[c(T,F,F),c(F,T,T)] > ? > Z0 > ? [1] 1.2 1.3 > ? > dim(Z0) > ? NULL > > whereas, of course, with > > ? > Z1<-Z[c(T,T,F),c(F,T,T)] > ? > Z1 > ? ? ? ?[,1] [,2] > ? [1,] ?1.2 ?1.3 > ? [2,] ?2.2 ?2.3 > ? > dim(Z1) > ? [1] 2 2 > > i.e. a fully-paid-up matrix. > > What I would have expected is that Z0 should come out as a 1x2 matrix: > > ? ? ? ? ?[,1] [,2] > ? [1,] ?1.2 ?1.3 > > with dim(Z0) --> [1] 1 2This seems to me yet another example of the side-effects caused by the automatic conversion of matrix to "vector" (in Splus/R sense) when the one of the dimension is 1. There are many examples of this sort. Of course the remedy is to do Z0 <- Z[c(T,F,F),c(F,T,T), drop=FALSE] Personally, I find this automatic conversion to "vector" a somewhat confusing feature (although I can see its reasons), and I know of many people that would have preferred that drop=FALSE was the default behaviour, but surely now is difficult to change it. regards Adelchi Azzalini -- Adelchi Azzalini <azzalini at stat.unipd.it> Dipart.Scienze Statistiche, Universit? di Padova, Italia http://azzalini.stat.unipd.it/
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? (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 ------------------------------