Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] is.matrix(), as.matrix, & as(,"matrix")

John Maindonald

2005-02-20

Replies:

Under help(matrix) it is written:

   'is.matrix' tests if its argument is a (strict) matrix. It is
   generic: you can write methods to handle specific classes of
   objects, see InternalMethods.

Further down, under "Details", the meaning of "strict" is explained
more explicitly:
   'is.matrix' returns 'TRUE' if 'x' is a matrix (i.e., it is _not_ a
   'data.frame' and has a 'dim' attribute of length 2) and 'FALSE'

(i.e., "strict" means "matrix" in a broad sense)

# The following is consistent with this:
> tab <- with(ToothGrowth, table(supp, dose))
> is.matrix(tab)
[1] TRUE
> class(as.matrix(tab))
[1] "table"

However the function as() has an argument "strict" that has the
connotation "restricted sense:

 strict: A logical flag. If 'TRUE', the returned object must be
      strictly from the target class (unless that class is a
      virtual class, in which case the object will be from the
      closest actual class (often the original object, if that
      class extends the virtual class directly).

# The following is consistent with this:
> class(as(tab,"matrix", strict=TRUE)) # TRUE is the default
[1] "matrix"
> class(as(tab,"matrix", strict=FALSE)) # TRUE is the default
[1] "table"

# Note also:
> class(data.matrix(tab))
[1] "table"

At the very least, the word "(strict)" should be removed from
   "'is.matrix' tests if its argument is a (strict) matrix."
and replaced by something like "(in the broad sense defined below)".

It would make for greater consistency and convenience if all of
as.matrix(), is.matrix() and data.matrix()
had arguments "strict", where "strict=FALSE" would preserve
the present behaviour.

Additionally, it would be useful to mention, under the documentation
for matrix() and data.matrix(), that as.matrix(tab) is equivalent to
as(tab, "matrix", strict=FALSE) (is that strictly correct?)

I often want to use xtable() with tables. There is no method
defined for the class "table". After a bit of rummaging, I found
that I can use:
xtable(as(tab, "matrix")).

John Maindonald         email: john.maindonald@(protected)
phone : +61 2 (6125)3473   fax : +61 2(6125)5549
Centre for Bioinformation Science, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.