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.
is.matrix
16 messages · Daniel Høyer Iversen, Tony Plate, Peter Dalgaard +4 more
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
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
-----Original Message----- From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of Peter Dalgaard Sent: Tuesday, November 11, 2008 1:31 PM To: Daniel H?yer Iversen Cc: r-devel at r-project.org Subject: Re: [Rd] is.matrix 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
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute? Hadley
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute? Hadley
Yes, I suspect a typo there.
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
That's confusing! In what situations is x a matrix but does not have a dim attribute?
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:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute? Hadley -- http://had.co.nz/
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>|
attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute?
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:
That's confusing! In what situations is x a matrix but does not have a dim attribute?
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,
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:
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute?
x = matrix(1,1,1) dim(x) = c(1,1,1)
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 confusing! In what situations is x a matrix but does not have
a dim attribute?
That was my point. I don't find it logical that is.matrix(a) gives FALSE but is.matrix(t( t(a) )) gives TRUE.
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).
I also think it would be more logical that a=c(1,1,2) dim(a) gives 3 1 instead of NULL,
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:
On Tue, Nov 11, 2008 at 1:42 PM, Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>|
attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have
a dim attribute?
x = matrix(1,1,1)
dim(x) = c(1,1,1)
I think you meant dim(x) <- c(3, 1) You created a 1 x 1 x 1 array.
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:
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>|
attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have
a dim attribute?
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
but is *not*
matrix (per is.matrix(...) and is("matrix", ...), consistently with the
docs).
(the redundant "vector" in is(x) could probably be removed?)
vQ
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
The point was that c(1, 1, 2) is not a matrix but a vector, two distinct things in R.
a <- c(1, 1, 2) class(a)
[1] "numeric"
ta <- t(a) class(ta)
[1] "matrix"
class(t(ta))
[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
-----Original Message----- From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of Daniel H?yer Iversen Sent: Tuesday, November 11, 2008 2:36 PM To: hadley wickham Cc: Tony Plate; r-devel at r-project.org Subject: Re: [Rd] is.matrix
That's confusing! In what situations is x a matrix but
does not have
a dim attribute?
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:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim | <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but
does not have
a dim attribute? Hadley -- http://had.co.nz/
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
On Tue, Nov 11, 2008 at 1:58 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
On Tue, Nov 11, 2008 at 1:42 PM, Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute?
x = matrix(1,1,1) dim(x) = c(1,1,1)
I think you meant dim(x) <- c(3, 1) You created a 1 x 1 x 1 array.
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.
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
-----Original Message----- From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of hadley wickham Sent: Tuesday, November 11, 2008 12:03 PM To: Wacek Kusnierczyk Cc: r-devel at r-project.org Subject: Re: [Rd] is.matrix On Tue, Nov 11, 2008 at 1:58 PM, Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
On Tue, Nov 11, 2008 at 1:42 PM, Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
hadley wickham wrote:
| is.matrix| returns |TRUE| if |x| is a matrix and has a |dim | <dim.html>| attribute of length 2) and |FALSE| otherwise
That's confusing! In what situations is x a matrix but does not have a dim attribute?
x = matrix(1,1,1) dim(x) = c(1,1,1)
I think you meant dim(x) <- c(3, 1) You created a 1 x 1 x 1 array.
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.
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 -- http://had.co.nz/
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel