a=c(1,1,2); is.matrix(a) gives FALSE is.matrix(t(a)) gives TRUE is.matrix(t(t(a))) gives TRUE Is this correct? Shouldn't all give FALSE? I think is.matrix should give FALSE when dimension is 1*n or n*1.
Daniel H?yer Iversen wrote:> a=c(1,1,2); > is.matrix(a) gives FALSE > is.matrix(t(a)) gives TRUE > is.matrix(t(t(a))) gives TRUE > > Is this correct? Shouldn't all give FALSE? > I think is.matrix should give FALSE when dimension is 1*n or n*1.No this is correct. is.matrix() returns TRUE if and only if the argument has a two-dimensional 'dim' attribute, and > dim(a) NULL> dim(t(a))[1] 1 3> dim(t(t(a)))[1] 3 1 (And is.array() depends on having a 'dim' attribute of positive length, so> dim(a) <- 3 > is.matrix(a)[1] FALSE> is.array(a)[1] TRUE ) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Daniel H?yer Iversen wrote:> a=c(1,1,2); > is.matrix(a) gives FALSE > is.matrix(t(a)) gives TRUE > is.matrix(t(t(a))) gives TRUE > > Is this correct? Shouldn't all give FALSE? > I think is.matrix should give FALSE when dimension is 1*n or n*1. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >All of the above is consistent with the documentation for is.matrix(): | is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>| attribute of length 2) and |FALSE| otherwise [There seems to be a typo in this sentence from ?is.matrix : an unmatched ")"] This is also useful behavior -- when programming it is often useful to know whether something is a matrix or not because that can affect computations performed with the object. For the more informal definition of "matrix" that it looks like want, you could use is.matrix(x) && all(dim(x)>1) (or maybe all(dim(x) != 1) depending on how you want to treat matrices that have a dimension with zero extent) -- Tony Plate