[R] Tables without names

Martin Maechler maechler at stat.math.ethz.ch
Fri Jun 12 14:32:07 CEST 2009


>>>>> "DM" == Duncan Murdoch <murdoch at stats.uwo.ca>
>>>>>     on Fri, 12 Jun 2009 06:09:14 -0400 writes:

    DM> On 11/06/2009 5:35 PM, Stavros Macrakis wrote:
    >> A table without names displays like a vector:
    >> 
    >> > unname(table(2:3)) [1] 1 1 1
    >> 
    >> and preserves the table class (as with unname in
    >> general):
    >> 
    >> > dput(unname(table(2:3))) structure(c(1L, 1L), .Dim =
    >> 2L, class = "table")
    >> 
    >> Does that make sense?  R is not consistent in its
    >> treatment of such unname'd tables:

    DM> One of the complaints about the S3 object system is that
    DM> anything can claim to be of class "foo", even if it
    DM> doesn't have the right structure so that foo methods
    DM> work for it.  I think that's all you're seeing here:
    DM> you've got something that is mislabelled as being of
    DM> class "table".  The solution is "don't do that".

indeed!

    >> In plot, they are considered erroneous input:
    >> 
    >> > plot(unname(table(2:3))) Error in xy.coords(x, y,
    >> xlabel, ylabel, log) : 'x' and 'y' lengths differ
    >> 
    >> but in melt, they act as though they have names 1:n:
    >> 
    >> > melt(unname(table(2:3))) indicies value 1 1 1 2 2 1
    >> 
    >> (By the way, is the spelling error built into too much
    >> code to be corrected?)
    >> 
    >> -s
    >> 
    >> PS What is the standard way of extracting just the
    >> underlying vector?  c(unname(...)) works -- is that what
    >> is recommended?

    DM> I would use as.numeric(), but I don't claim it's
    DM> standard.

many months ago in a discussion about the use (and "misuse") of 
c() for coercing arrays/matrices to (atomic) vectors, Brian
Ripley  I think  advertized the use of as.vector() 
with which I strongly agree.

as.vector() here has the additional advantage of *not*
transforming integer into double.

Martin Maechler, ETH Zurich




More information about the R-help mailing list