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

2005-02-20 Thread John Maindonald
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: [EMAIL PROTECTED]
phone : +61 2 (6125)3473fax  : +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@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] is.matrix(), as.matrix, as(,matrix)

2005-02-20 Thread Gabor Grothendieck
John Maindonald john.maindonald at anu.edu.au writes:

: 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)).

That's probably ok for most practical purposes but it will
not preserve the table's dnn, if it has one:

R tab - table(1:3, dnn = A)
R tab  # dnn present
A
1 2 3 
1 1 1 
R as.matrix(tab)  # dnn gone
  [,1]
11
21
31

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html