Skip to content

is.matrix

16 messages · Daniel Høyer Iversen, Tony Plate, Peter Dalgaard +4 more

#
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:
No this is correct. is.matrix() returns TRUE if and only if the argument
has a two-dimensional 'dim' attribute, and

 > dim(a)
NULL
[1] 1 3
[1] 3 1

(And is.array() depends on having a 'dim' attribute of positive length, so
[1] FALSE
[1] TRUE

)
#
Daniel H?yer Iversen wrote:
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
#
And the other missing piece is that t() coerces the input vector to a
1-column matrix that can then be transposed and returned as a 1-row matrix
(with a dim attribute).

-Christos
#
That's confusing!  In what situations is x a matrix but does not have
a dim attribute?

Hadley
#
hadley wickham wrote:
Yes, I suspect a typo there.
#
That was my point. I don't find it logical that
is.matrix(a) gives FALSE but
is.matrix(t( t(a) )) gives TRUE.

I also think it would be more logical that
a=c(1,1,2)
dim(a) gives 3 1 instead of NULL,



Daniel
On Tue, Nov 11, 2008 at 8:21 PM, hadley wickham <h.wickham at gmail.com> wrote:
#
hadley wickham wrote:
x = matrix(1,1,1)
dim(x) = c(1,1,1)
is.matrix(x)
# no
is("matrix", x)
# no
is(x)
# hm...

following the last, there would be a situation in which an object is a
matrix (per is(...)) and has a dim attribute of length != 2, but is a
matrix (per is.matrix(...) and is("matrix", ...), consistently with the
docs).

(the redundant "vector" in is(x) could probably be removed?)

vQ
#
On Tue, Nov 11, 2008 at 1:35 PM, Daniel H?yer Iversen
<danielho at stud.ntnu.no> wrote:
In R t(t(a)) != a, because a vector is different to a 1d matrix.  This
is different to mathematical convention - you could argue that it
would have been wise to stick with convention, but there are some good
reasons for treating vectors differently to 1d matrices, and its too
late to change now.

Note that the following are all different in R:

a <- 1:3
b <- array(1:3, 3)
c <- array(1:3, c(3,1))
d <- array(1:3, c(3,1,1))
e <- array(1:3, c(3,1,1,1))
f <- array(1:3, c(3,1,1,1,1))

Hadley
#
On Tue, Nov 11, 2008 at 1:42 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
I think you meant
dim(x) <- c(3, 1)

You created a 1 x 1 x 1 array.

Hadley
#
Daniel H?yer Iversen wrote:
that's a different story, because t() performs an implicit cast from
vector to matrix, so you have

t(vector) -> matrix
t(matrix) -> matrix
t(t(vector)) -> matrix

interestingly,

x = 1:2
dim(x) = 2

adds "matrix" to is(x), but it is still not is.matrix(x) (consistently
with the docs).
well, it might give 3 1 1, or 3 1 1 1, or ..., which all could be
considered logical.
i think dim(x) giving just 3 would be fine.

vQ
#
hadley wickham wrote:
i know, that's precisely what i wanted.  this is a simple 3d structure,
yet is(x) reveals it is a matrix.  that was the point.

some further observations:

x = as.list(1:2)
is(x)
# x is a list

dim(x) = 2
is.list(x)
# sure
is(x)
# not so sure any more -- a matrix?

vQ
#
Wacek Kusnierczyk wrote:
but is *not*
#
The point was that c(1, 1, 2) is not a matrix but a vector, two distinct
things in R.
[1] "numeric"
[1] "matrix"
[1] "matrix"

The transpose operation is only defined for matrices, so if you insist to
apply it to a vector, it first needs to be coersed to a matrix and then
transposed.  The result of this operation is a matrix. The result of
transposing this matrix will still be a matrix.

-Christos
#
On Tue, Nov 11, 2008 at 1:58 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
I think the message here is not to use is().  is() is designed for use
with S4 objects, and it doesn't always give what you'd expect for
primitive objects.  This isn't normally a problem because for those
objects you use is.matrix(), is.array(), is.vector() etc.  Sure, this
isn't a particularly wonderful aspect of the language, but there are
far more interesting and important things for people to spend their
time on, and fixing these behaviours now could potentially break large
amounts of existing code, with little benefit.

Hadley
#
S+, now and as far back as 3.4 (1996) and S versions 3 (c. 1990)
and 4 (1999) define is.matrix as
   function(x)length(dim(x)) == 2
so is.matrix(data.frame(x=1:3,y=4:6)) returns TRUE.
It essentially means that you can use 2 subscripts on
the object (it assumes that the dim() method for class(x)
is defined appropriately).

We never changed it to look at the S4 class or the dim attribute
directly because that broke old code.

Why do people call is.matrix(x)?

Bill Dunlap
TIBCO Spotfire Inc
wdunlap tibco.com